Get friendly Url from Url property

Vote:
 

I'm having a problem when trying to render an Url from an Url property in my view. Problem is that I want the friendly URL either it's an internal nor an external link. I can get the external link correct only by typing @Url.PageUrl(Model.Link.ToString()) but for the internal link this only gives me the ID to the page. How to get this done for both internal and external links?

#85441
Apr 25, 2014 10:30
Vote:
 

If you are using MVC, then I would suggest to use @Url.Content()

#85478
Apr 27, 2014 6:53
Vote:
 

You can also us UrlResolver.current. As well

#85480
Apr 28, 2014 5:16
Vote:
 

Assuming that you've defined the link property like this:

[BackingType(typeof(PropertyUrl))]
public virtual Url Link { get; set; }

    

You can use the following code in your view:

@Url.ContentUrl(Model.CurrentPage.Link)

    

Or you can use the following code in your controller:

var urlHelper = ServiceLocator.Current.GetInstance<UrlHelper>();
var friendlyUrl = urlHelper.ContentUrl(currentPage.Link);

    

Hope this helps.

#85515
Edited, Apr 28, 2014 22:44
Vote:
 

Thanks, found what I was looking for here.

This is one of the most confusing areas of EPiServer, getting urls to things. UrlBuilder, UrlResolver, UrlHelper, UrlRewriteProvider, PermanentLinkUtility, PermanentLinkMapStore etc. It's just too many options. It shouldn't have to be this hard.

#86337
May 19, 2014 16:34
Vote:
 

UrlResolver should be your first stop in EPiServer 7.5+.  the others are from previous versions.  Hopefully that helps you decide which on to use.

#86338
May 19, 2014 16:36
Vote:
 

Aha, is that so.

In my case the only thing that made sense was UrlHelper.

I have an Url property that I want to get the link for, outside of a view. How am I supposed to do it?

#86339
May 19, 2014 16:55
Vote:
 

UrlHelper uses UrlResolver under the hood

public static string ContentUrl(this UrlHelper urlHelper, Url url)
{
    if (url == null || url.IsEmpty())
    return string.Empty;
    else
    return ServiceLocator.Current.GetInstance<UrlResolver>().ConvertClassicToContentUrl(url.ToString()) ?? url.ToString();
}



#86340
May 19, 2014 16:59
Vote:
 

Ah, right, thanks!

#86341
May 19, 2014 17:05
Vote:
 

Turns out ConvertClassicToContentUrl cannot be accessed, it's internal.

And it in turn uses UrlBuilder. :-P

#86342
May 19, 2014 17:07
Vote:
 

Right, that is why they wrapped it in a helper "CotnentUrl".  

#86343
May 19, 2014 17:09