Copied to clipboard

Flag this post as spam?

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


  • Francesco 9 posts 89 karma points
    Feb 08, 2023 @ 08:55
    Francesco
    0

    Sitemap with custom controller

    Hi,

    I'm trying to create a custom controller with a custom route but I can't get the Umbraco Context.

    This controller must render the sitemap of my website.

    This is how I register the route

    and this is the controller:

    In the ActionResult GetSitemapIndex I can't access the UmbracoContext: enter image description here

    What am I doing wrong? I hope the question is specific enough.

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Feb 08, 2023 @ 12:12
    Huw Reddick
    0

    Hi Francesco,

    You already have the UmbracoContext passed in using DI, you just need to assign it to a local variable

    private readonly IUmbracoContextAccessor _context;
    

    Then in the constructor just assign it

    _context = umbracoContextAccessor;
    
  • Francesco 9 posts 89 karma points
    Feb 08, 2023 @ 13:03
    Francesco
    0

    Thank you for your help!

    Unfortunately it does not work, any other ideas? enter image description here

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Feb 08, 2023 @ 14:33
    Huw Reddick
    0

    Hi,

    What is ISitemapService ? It is that that is erroring, I have not come across this service in Umbraco.

  • Francesco 9 posts 89 karma points
    Feb 08, 2023 @ 14:37
    Francesco
    0

    It's a custom service written by me, and it use another class that return false in this if statement

    if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext) || umbracoContext.Content == null)
    
  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Feb 08, 2023 @ 15:02
    Huw Reddick
    0

    Ok thanks for clarifying, so it is this service that is not getting an umbraco context rather than your controller.

    What does your service code look like?

  • Francesco 9 posts 89 karma points
    Feb 13, 2023 @ 15:38
    Francesco
    0

    My service is this: enter image description here And in baseRepository: enter image description here

    I have the function that try to read umbracocontext: enter image description here

    The problem is that even if I use the UmbracoContextAccessor in the SitemapController during a debug session (I'm using Immediate Window) it says that "it wasn't able to get an UmbracoContext" like the screen before.

Please Sign in or register to post replies

Write your reply to:

Draft