EPiServer - how to get external page url

We use a shared database in the developer environment AKA localhost.

This is how my site settings look like:

EPiServer Site Settings

Generating an external URL can be a bit tricky. Here are a couple of scenarios:

  • I'd like to get http://localhost/something when the site is running on my machine
  • I'd like to get http://mydomain.com/something when the site is running on a remote machine (a test server, for example)
  • I'd like to get http://mydomain.com/something when I'm generating an external URL from a scheduled task.

To achieve this, I prepared a simple utility class:

public static class UrlUtils
    public static 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;

And this is how it looks in action:

EPiServer external page url

comments powered by Disqus