Anders G. Nordby

Senior Systems Consultant at CGI

Getting an External URL in EPiServer 7.x

This seems like a pretty straight-forward task, but it’s actually harder than you could expect.
It’s such a basic need that I think it should have been built in.

Here is my working version (after many failed attemps):

private static string GetExternalUrl(IContent content)
{
    var urlResolver = ServiceLocator.Current.GetInstance();
    var internalUrl = urlResolver.GetUrl(content.ContentLink);
            
    var url = new UrlBuilder(internalUrl);
    Global.UrlRewriteProvider.ConvertToExternal(url, null, System.Text.Encoding.UTF8);

    var friendlyUrl = UriSupport.AbsoluteUrlBySettings(url.ToString());
    return friendlyUrl;
}

UPDATE (improved version from Ted Nybergs comment):

private static string GetExternalUrl(IContent content)
{
    var internalUrl = UrlResolver.Current.GetUrl(content.ContentLink);
            
    var url = new UrlBuilder(internalUrl);
    Global.UrlRewriteProvider.ConvertToExternal(url, null, System.Text.Encoding.UTF8);

    var friendlyUrl = UriSupport.AbsoluteUrlBySettings(url.ToString());
    return friendlyUrl;
}

UPDATE:
For those who think the GetUrl method will give me what I want… Take a look at the content of my ext variable here, and compare it to what the method will return! Nope, the GetUrl method does not give me the external URL:
friendlyUrl

Advertisements

7 responses to “Getting an External URL in EPiServer 7.x

  1. Ted Nyberg 2014-04-29 at 16:29

    In recent versions you can use UrlResolver.Current.GetUrl(somePage.ContentLink)

    Cheers!

  2. Anders G. Nordby 2014-04-29 at 16:35

    This returns the internal URL, but it does improve/shorten my code a bit.

  3. Johan Björnfot (@jbearfoot) 2014-04-29 at 17:35

    GetUrl with just a ContentReference as a parameter will return a url in the same context as the current request (that is it the current request in onpageedit then the url will be in onpageedit format).
    If you always want the link to be in view/default format you could use the overload that takes a VirtualPathArgument as
    GetUrl(contentLink, null, new VirtualPathArguments() { ContextMode = ContextMode.Default });

  4. Smithk2 2014-08-12 at 22:58

    This website is mostly a walkby for all the info you wished about this and didnt know who to ask. Glimpse right here, and also youll undoubtedly uncover it. cdfffcfddedddedd

  5. Paul Smith 2015-01-13 at 15:05

    Using UriSupport.AbsoluteUrlBySettings to make the Url can cause problems in production sites as depending on the config it may generate urls with the internal hosting url (if using EPiServer hosting for example).

  6. Free Calendars 2016-01-16 at 09:10

    I simply couldn’t go away your website prior to suggesting that I extremely enjoyed the standard info an individual
    provide in your guests? Is gonna be back steadily in order to check
    out new posts

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: