Try our conversational search powered by Generative AI!

Get the PageReference Object from the friendly URL(string).


How can we get the PageReference Object from the friendly URL(string). I tried PermanentLinkMapStore, but failed... Can any body please guide.


Jul 09, 2010 17:59

I believe you need to play around with the URL rewriter to break a URL down into its internal representation - this contains an "id" parameter that you can grab a PageReference from.

Something like the code below should sort you out:

//* Convert an external URL to an internal URL           
UrlBuilder newURL = new UrlBuilder("http://EPiServer6TestSite/Home-page/");

//* Now fetch the ID and get a page reference
PageReference result;
string pageIDslug = newURL.QueryCollection["id"];
if (!string.IsNullOrEmpty(pageIDslug))
    string[] splitSlug = newURL.QueryCollection["id"].Split(new[] { '_' }, StringSplitOptions.RemoveEmptyEntries);

    int pageId;
    if (splitSlug.Length > 0 && int.TryParse(splitSlug[0], out pageId))
        //* Do we have a work page ID?
        int workPageId;
        if (splitSlug.Length > 1 && int.TryParse(splitSlug[1], out workPageId))
            result = new PageReference(pageId, workPageId);
            result = new PageReference(pageId);

Jul 09, 2010 18:42

PageReference.ParseURL() could be another choice. Need to convert it to an internal URL first though.

Jul 09, 2010 19:24

Thanks, combination of both suggestions solved my problem

Jul 13, 2010 11:52


Another solution - very similar to above too . It has same concept to covert the Url to internal state.

 UrlBuilder urlBuilder = new UrlBuilder(Url);
                    object pageReference;

                    Global.UrlRewriteProvider.ConvertToInternal(urlBuilder, out pageReference);

                   //or use PageReference.ParseUrl() here

                      if (pageReference != null && pageReference is PageReference)
                       //ur page reference logic here

Edited, Jul 13, 2010 23:14

"/mysite/en-gb/child/grandchild""/mysite/en-gb/child/grandchild"On a similar note, a complete newbie question by I can't seem to find the answer to this...

How do I get a reference to a page by absolute URL, e.g. "/mysite/child/grand-child"

PageReference.ParseURL looks like the correct method, but what am I missing?

            var relUrl = "/CommittedQuitters/Today/Blow-Temptation/";
            //var pageRef = PageReference.ParseUrl(relUrl);
            //var pageByLoc = DataFactory.Instance.GetPage(pageRef);

var url = "/mysite/child/grand-child";
var pageRef = PageReference.ParseUrl(url); <-- returns PageReference.EmptyReference
var page = DataFactory.Instance.GetPage(pageRef);


Eventually I will end up with several sites and languages, so the tree structure would be like so for example, so ideally i'd like to be able to retrieve the reference relatively...





Let me know if you need info.


Oct 19, 2010 17:30
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.