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; }
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.
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.
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.
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;
}
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}";
}
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;
}
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();
}
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