EPiServer - how to get external page url

When developing EPiServer websites, usually my developer machine (localhost) and test server share the same database / blobs.

This is how my site settings look like:

EPiServer Site Settings

I want to get an external URL of the page, but I want to get:

http://localhost/something when I'm browsing the website from localhost.
http://mydomain-test.com/something when I'm browsing the website from test server.

When the code is executed from a new thread (scheduled jobs, etc.), I want to have http://mydomain-test.com/something (Site URL from admin mode + relative path).

Please note that I have wildcard in the list of host names, which is used in 'new thread' scenario.

Code:

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