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

Denna artikel har migrerats från en tidigare version av vår webbplats och kan därför avvika i utseende och funktionalitet.

Den här artikeln är inte översatt till svenska och visas därför på engelska istället.


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.

Uppskattad lästid : 2 minuter

Gå till avsnitt

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)