How to route an url to IContent?

Member since: 2007

Hi,

I have an url to a document in the internal format (/episerver/CMS/Content/globalassets/test/requirements.xlsx,,824?epieditmode=False), how can I get the corresponding IContent from that url?

I've tried with UrlResolver without any luck :(

var content = UrlResolver.Current.Route(new UrlBuilder(urlAsString));
#91342 Oct 03, 2014 4:47
  • Member since: 2011

    Hi,

    Did you try with 

    UrlResolver.Current.Route(new UrlBuilder(urlAsString), ContextMode.Edit); ? If you pass the ContextMode parameter, it will use Default.

    Regards,

    /Q

    #91343 Oct 03, 2014 6:04
  • Member since: 2007

    Yes. Tried with both.

    #91345 Oct 03, 2014 6:18
  • Member since: 2004

    If it is a custom IContent implemention you need to implement EPiServer.Web.IRoutable. 

    Also UrlResolver.GetUrl will return null if the content does not have a page template (meaning the link will end up in a 404 anyway), you can disable template validation for UrlResolver by using overload that takes a VirtualPathArguments and in there set ValidateTemplate=false 

    #91353 Oct 03, 2014 9:48
  • Member since: 2007

    Maybe I was a bit unclear. I don't want to generate the URL, I want to resolve the IContent from an URL. In this case it's a MediaData, hence no template, but that should only matter if I want to generate the URL I guess. And there is no overload that takes VirtualPathArguments.

    #91366 Oct 03, 2014 11:56
  • Member since: 2004

    As a side note: MediaData actually has a template, in that case the template is a HttpHandler (ContentMediaHttpHandler).

    If you want to resolve a content from a url you would use UrlResolver.Route(UrlBuilder) that should handle media.

    #91367 Oct 03, 2014 12:04
  • Member since: 2007

    Alright. Then its even more strange that the above code doesn't work. I think I have to set up some more isolated tests with the above URL.

    #91368 Oct 03, 2014 12:11
  • Member since: 2007

    Hi again,

    Now it seems to work when adding ContextMode.Edit as well. Not sure why it didn't work the first time.

    #91400 Oct 03, 2014 17:35