Copied to clipboard

Flag this post as spam?

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


  • Adriano Fabri 469 posts 1633 karma points
    Jun 20, 2023 @ 17:05
    Adriano Fabri
    0

    Umbraco 11 - How to retrieve an IPublishedContent by URL in a Partial View?

    Hi, there is a way in a partial view to retrieve a content by URL?

    In v8 we could use umbracoContext.Content.GetByRoute(url); but I didn't find an equivalent method in v11.4

    Can anyone help me?

    Thank you

    Adriano

  • Adriano Fabri 469 posts 1633 karma points
    Jun 20, 2023 @ 17:31
    Adriano Fabri
    100

    Founded.

    I need to injet the UmbracoContextAccessor

    @inject IUmbracoContextAccessor UmbracoContextAccessor
    

    and use the UmbracoContextAccessorExtension to get the Umbraco Context and to use its methods

    var test = UmbracoContextAccessorExtensions.GetRequiredUmbracoContext(UmbracoContextAccessor)?.Content?.GetByRoute(megaNavItem.Url);
    

    Hope that this can help anyone

    Adriano

  • Tor Langlo 191 posts 554 karma points
    Jun 20, 2023 @ 20:18
    Tor Langlo
    0

    I think you can also access the UmbracoContext directly, like this:

    var test = this.UmbracoContext.Content.GetByRoute(megaNavItem.Url);
    
  • Adriano Fabri 469 posts 1633 karma points
    Jun 21, 2023 @ 08:27
    Adriano Fabri
    0

    I tried but received these errors:

    • error CS0103: UmbracoContext not exists in current context
    • error CS1061: ViewsPartialsLayout_TopMenu does not contain a definition of UmbracoContext
Please Sign in or register to post replies

Write your reply to:

Draft