Obtain friendly URL of page



I have a PageReference property and I want o obtain the URL to that page. I'm using EpiServer 9.

Initially I was using: EPiServer.Web.Routing.UrlResolver.Current.GetUrl(myPageReference); but this method can return the url in this format “/EPiServer/CMS/Content/mypage,,74/?epieditmode=True”  instead on "/myPage" (when we are in edit mode).

I saw some suggestions to use this: EPiServer.Web.Routing.UrlResolver.Current.GetUrl(myPageReference, null, new EPiServer.Web.Routing.VirtualPathArguments() { ContextMode = EPiServer.Web.ContextMode.Default });, which seems that is working to obtain the frienly URL.

Is this the proper/best way to do it, or is there another method in this Epi version?



May 31, 2016 14:59

I've been using this code the get external URL:

public string GetExternalUrl(PageReference pageReference)
    var internalUrl = UrlResolver.Current.GetUrl(pageReference);
    var url = new UrlBuilder(internalUrl);
    Global.UrlRewriteProvider.ConvertToExternal(url, null, Encoding.UTF8);

    string externalUrl = HttpContext.Current == null
        ? UriSupport.AbsoluteUrlBySettings(url.ToString())
        : HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + url;

    return externalUrl;
May 31, 2016 15:08

I was hoping that Episerver 9 would have a method for getting the URL

Jun 01, 2016 9:35

I'm going to keep posting my blog post until Episerver implements this functionality in UrlResolver. Please see http://dodavinkeln.se/post/how-to-get-the-external-url-to-content.

Jun 01, 2016 17:41

Thanks Johan, so I should understand that EPi doesn't have anything implemented this functionality and we still need to use a custom method for this.

Jun 02, 2016 10:27

It depends on what you mean. The functions used in my code are built-in.

Jun 02, 2016 10:35

The only "hacky" part in my code is to obtain the absolute URL. So if you only want the friendly URL (which might be relative) you can remove the last part in the code. You can also set default values on the language parameter.

Jun 02, 2016 11:22
* 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.