Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Obtain friendly URL of page

Vote:
 

Hello,

I have a PageReference property and I want o obtain the URL to that page. I'm using EpiServer 9.

Initially I was using: EPiServer.Web.Routing.UrlResolver.Current.GetUrl(myPageReference); but this method can return the url in this format “/EPiServer/CMS/Content/mypage,,74/?epieditmode=True”  instead on "/myPage" (when we are in edit mode).

I saw some suggestions to use this: EPiServer.Web.Routing.UrlResolver.Current.GetUrl(myPageReference, null, new EPiServer.Web.Routing.VirtualPathArguments() { ContextMode = EPiServer.Web.ContextMode.Default });, which seems that is working to obtain the frienly URL.

Is this the proper/best way to do it, or is there another method in this Epi version?

Thanks,

Bogdan

#149405
May 31, 2016 14:59
Vote:
 

I've been using this code the get external URL:

public 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;
}
#149407
May 31, 2016 15:08
Vote:
 

I was hoping that Episerver 9 would have a method for getting the URL

#149446
Jun 01, 2016 9:35
Vote:
 

I'm going to keep posting my blog post until Episerver implements this functionality in UrlResolver. Please see http://dodavinkeln.se/post/how-to-get-the-external-url-to-content.

#149489
Jun 01, 2016 17:41
Vote:
 

Thanks Johan, so I should understand that EPi doesn't have anything implemented this functionality and we still need to use a custom method for this.

#149510
Jun 02, 2016 10:27
Vote:
 

It depends on what you mean. The functions used in my code are built-in.

#149511
Jun 02, 2016 10:35
Vote:
 

The only "hacky" part in my code is to obtain the absolute URL. So if you only want the friendly URL (which might be relative) you can remove the last part in the code. You can also set default values on the language parameter.

#149514
Jun 02, 2016 11:22
Vote:
 

How can I make this work from a helper that gets called inside episerver? I'm trying to get the friendly url inside episerver when the page is in edit mode. This currently reutns the episerver internal url.

/// <summary>
/// Get EPiServer External Url
/// </summary>
/// <param name="contentReference"></param>
/// <returns></returns>
private static string GetEPiServerExternalUrl(ContentReference contentReference)
{
    var internalUrl = UrlResolver.Current.GetUrl(contentReference);
    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;
}
#246845
Edited, Jan 14, 2021 20:08
Vote:
 

Try this

public static string GetUrlAbsolute(ContentReference contentLink, UrlResolver urlResolver = null)
{
    if (urlResolver == null)
    {
        urlResolver = ServiceLocator.Current.GetInstance<UrlResolver>();
    }

    var relativeUrl = urlResolver.GetUrl(contentLink);

    return GetUrlAbsolute(relativeUrl);
}

public static string GetUrlAbsolute(string relativeUrl)
{
    return $"{HttpContext.Current.Request.Url.Scheme}://{HttpContext.Current.Request.Url.Host}{relativeUrl}";
}
#246975
Jan 17, 2021 6:20
Eric - Jan 18, 2021 14:19
When I call this I get: http://localhost/EPiServer/CMS/Content/,,98_481302/?epieditmode=True

Instead of this: http://localhost/
Vote:
 

If you are on one of the newest (don't remember version number), then you can do this

public static string GetUrlAbsolute(ContentReference contentLink, UrlResolver urlResolver = null)
{
    if (urlResolver == null)
    {
        urlResolver = ServiceLocator.Current.GetInstance<UrlResolver>();
    }

    var url= urlResolver.GetUrl(contentLink, new UrlResolverArguments()
			{
				ForceAbsolute = true,
			});

    return url;
}

EDIT: Eric Dierkens I didn't read your question, only looked at the previous answer.

To always get the friendly url and not epi internal url when you are in edit mode you need to add this in the UrlResolverArguments()

ContextMode = ContextMode.Default

So the code will then be (if you also want to get it with the absolute url
public static string GetUrlAbsolute(ContentReference contentLink, UrlResolver urlResolver = null)
{
    if (urlResolver == null)
    {
        urlResolver = ServiceLocator.Current.GetInstance<UrlResolver>();
    }

    var url= urlResolver.GetUrl(contentLink, new UrlResolverArguments()
			{
                        ContextMode = ContextMode.Default,
				ForceAbsolute = true,
			});

    return url;
}
#246981
Edited, Jan 17, 2021 10:53
Eric - Jan 18, 2021 14:40
This worked with a little tweaking. See Below.
Praful Jangid - Jan 18, 2021 14:45
Cool... I didn't know about ContextMode.
Thanks for sharing that Sebbe.
Vote:
 

Final solution that worked for me.

/// <summary>
/// Get Ur lAbsolute From Inside EPiServer
/// </summary>
/// <param name="contentLink"></param>
/// <param name="language"></param>
/// <param name="urlResolver"></param>
/// <returns></returns>
private static string GetUrlAbsoluteFromInsideEPiServer(ContentReference contentLink, string language, UrlResolver urlResolver = null)
{
        if (urlResolver == null)
        {
            urlResolver = ServiceLocator.Current.GetInstance<UrlResolver>();
        }

        var url = urlResolver.GetUrl(contentLink: contentLink, language: language, virtualPathArguments: new VirtualPathArguments()
        {
            ContextMode = ContextMode.Default,
            ForceAbsolute = true
         }) ;

         return url.ToString();
}
#247030
Jan 18, 2021 14:43
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.