Extension for getting friendlyUrl when all you have is the internal url in EpiServer

January 23rd, 2012

Here’s a simple extension method for EPiServer when you need to get the friendly url from an internal url.

Note the last line checking if the Uri is an absoluteUri first. If you try to get the AbsoluteUri if it’s not you will get the following exception:
InvalidOperationException: This operation is not supported for a relative URI. This operation is not supported for a relative URI.

?View Code CSHARP
1
2
3
4
5
6
public static string GetFriendlyURL(this string internalURL)
{
   var url = new UrlBuilder(internalURL);
   EPiServer.Global.UrlRewriteProvider.ConvertToExternal(url, null, System.Text.UTF8Encoding.UTF8);
   return url.Uri.IsAbsoluteUri ? url.Uri.AbsoluteUri : url.Uri.OriginalString;
}

Leave a Reply