Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Keyur Shah 14 posts 44 karma points
    Jan 06, 2017 @ 07:07
    Keyur Shah
    0

    How to get IContent from the Url or by route

    I am trying to get the IContent from the URL

    For Example

    I have url http://localhost:8080/abc/xyz then how to get the IContent from the url above in umbraco 7 and above.

    I have tried

    var content = UmbracoContext.Current.ContentCache.GetByRoute("http://localhost:8080/abc/xyz ");

    or

    var content = UmbracoContext.Current.ContentCache.GetByRoute("/abc/xyz ");

    but no luck.

  • Marc Goodson 2157 posts 14435 karma points MVP 9x c-trib
    Jan 06, 2017 @ 19:16
    Marc Goodson
    101

    Hi Keyur

    The UmbracoContext.Current.ContentCache.GetByRoute accepts a simple path eg your second option:

    var content = UmbracoContext.Current.ContentCache.GetByRoute("/abc/xyz");
    

    see: https://our.umbraco.org/apidocs/csharp/api/Umbraco.Web.PublishedCache.IPublishedContentCache.html

    however if you have a domain set on your Umbraco tree, then the route needs to include the Id of the domain node in order to work eg

    var content = UmbracoContext.Current.ContentCache.GetByRoute("123/abc/xyz");
    

    Where 123 is the id of the node the domain is set on

    Have a look at Tim Geyssens' UmbracoPageNotFoundHandler plugin source as a good example of this in use:

    https://github.com/TimGeyssens/UmbracoPageNotFoundManager/blob/master/PageNotFoundManager/PageNotFoundContentFinder.cs

    hope that helps

    regards

    Marc

  • Keyur Shah 14 posts 44 karma points
    Jan 09, 2017 @ 07:03
    Keyur Shah
    0

    Thanks Marc,

    This really helps.

    Though, when we have multiple content at root lever / same level at the site and no domain associated with it. In such case how to find the content similar to example above.

    Thanks, Keyur

  • Marc Goodson 2157 posts 14435 karma points MVP 9x c-trib
    Jan 15, 2017 @ 12:19
    Marc Goodson
    0

    Hi Keyur

    I've not tried to get a route when there are multiple top level nodes without domains.

    I've just create a dummy site based on the Fanoe starter kit, and created multiple such top level nodes.

    I'm finding in this circumstance the

    var content = UmbracoContext.Current.ContentCache.GetByRoute("/abc/xyz");
    

    approach works.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies