If you're on EPiServer 8, you should use the UrlResolver:
var content = UrlResolver.Current.Route(new UrlBuilder(linkItem.Href))
You also need to null-check content before returning content.ContentLink.
Thanks Johan, you have made me learn something new, that is a nice way to solve this.
Do you know if it works in 7.5 also?
Hi guys,
Valdis - all of these links fail to return a ContentReference using the original method. They all returned a valid ContentReference in 7.5 so I'm not sure what's happened, but it is a concern!
Johan - that method works a treat. Thankfully we're in a position to upgrade our own nuget libraries to version 8, so I now have two simplified methods to cover LinkItem and Url:
public static ContentReference ToContentReference(this LinkItem linkItem) { if (linkItem == null) return ContentReference.EmptyReference; var content = UrlResolver.Current.Route(new UrlBuilder(linkItem.Href)); return content != null ? content.ContentLink : ContentReference.EmptyReference; }
And
public static ContentReference ToContentReference(this Url url) { if (url == null) return ContentReference.EmptyReference; var content = UrlResolver.Current.Route(new UrlBuilder(url)); return content != null ? content.ContentLink : ContentReference.EmptyReference; }
Thanks for your help guys. Still very interested to know why this broke in 8. I can't say I analyse all of the Release notes as much as I might, but it seems a fairly fundamental piece of the core of EPiServer that now works differently.
Al
Mapped links are gone in EPiServer 8. That's why your first method fails on PermanentLinkMapStore.ToMapped(). Your second method finds the permanent link, which stil works.
This information was in the documentation under breaking changes :)
In which case...tut tut to me for not not viewing the release notes properly. However, if that's the case I would have expected the ToMapped and TryToMapped methods to have been marked obsolete, or perhaps even removed entirely if they no longer work - do you think so? Perhaps I should raise this.
Thanks again
Al
It has been discussed here as well http://world.episerver.com/Modules/Forum/Pages/Thread.aspx?id=117930. But please ping the product guys, because I didn't get an answer when I raised the problem with functions related to mapped links.
Hi all,
I'm interested in your view on the following. In certain circumstances it is useful for us to know whether the LinkItem's within a LinkItemCollection are internal objects i.e. pages/assets etc stored in EPiServer. For sometime we've had this method in our library:
However, when I ran some tests on it today I found that even though PermanentLinkMapStore.ToMapped(urlBuilder) returned true, indicating it could map the Url to an internal object, PermanentLinkUtility.GetContentReference(urlBuilder) still returned an empty ContentReference - which seems incredibly strange to me.
So I've re-written it to the following. I'm not as happy with it as it has a dependency on the ContentLoader however it does seem to work:
Interested to hear youir thoughts.
Thanks in advance
Al