How to get the friendly URL of a page in EPiServer CMS

This article was migrated from an older iteration of our website, and it could deviate in design and functionality.


This post explains how to get the friendly URL of a page in EPiServer, handy for those cases where you won't get automatic rewriting of internal URLs.

Estimated read time : 2 minutes

Jump to

Maybe there are other posts about this, but I thought I'd share an easy way of getting the friendly URL of an EPiServer page since I've gotten questions about it on numerous occasions.

In EPiServer CMS 6

public static string GetFriendlyURL(PageReference PageLink, string URL)
{
   var url = new UrlBuilder(URL);
 
   EPiServer.Global.UrlRewriteProvider.ConvertToExternal(url, PageLink, System.Text.UTF8Encoding.UTF8);
 
   return url.Uri.AbsoluteUri;
}

Note: you do not have to specify the PageLink parameter when calling the ConvertToExternal() method, but according to the SDK you will get the best performance when specifying both the PageLink parameter as well as the internal URL. So, you should probably not ignore the PageLink parameter unless you have to - URL rewriting is probably the most expensive operation carried out by EPiServer.

In EPiServer 7

In EPiServer 7 we make use of a UrlResolver to get the URL of a page:

public static string GetFriendlyURL(ContentReference pageLink)
{
   return ServiceLocator.Current.GetInstance<UrlResolver>()
                                .GetVirtualPath(pageLink);
}

If you have an HtmlHelper available, such as in an ASP.NET MVC view, you can use the PageUrl method:

@Url.PageUrl(Model.CurrentPage.LinkURL)