Copied to clipboard

Flag this post as spam?

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


  • Andreas Kristensen 65 posts 269 karma points c-trib
    Nov 24, 2021 @ 10:33
    Andreas Kristensen
    0

    No Umbraco Context available when implementing IVirtualPageController

    I am trying to implement an IVirtualPageController to serve some dynamic XML sitemaps. I have pretty much been following the documentation here, but I simply cannot get an Umbraco Context in the FindContent method.

    Here is my controller, implementing IVirtualPageController:

    public class SitemapController : UmbracoPageController, IVirtualPageController
    {
        private readonly IUmbracoContextAccessor _umbracoContextAccessor;
    
        public SitemapController(ILogger<SitemapController> logger, ICompositeViewEngine compositeViewEngine, IUmbracoContextAccessor umbracoContextAccessor) : base(logger, compositeViewEngine)
        {
            _umbracoContextAccessor = umbracoContextAccessor;
        }
    
        public IPublishedContent FindContent(ActionExecutingContext actionExecutingContext)
        {
            IPublishedContent siteRoot = null;
    
            if (_umbracoContextAccessor.TryGetUmbracoContext(out var context))
            {
                siteRoot = context.Content.GetAtRoot().First();
            }
    
            return siteRoot;
        }
    
        [HttpGet]
        public ActionResult SitemapIndex()
        {
            //Removed for brevity
    
            Response.ContentType = "text/xml";
            return View("SitemapIndex", sitemapIndexViewModel);
        }
    }
    

    Here is the composer where I register the new route:

    public class SitemapComposer : IComposer
    {
        public void Compose(IUmbracoBuilder builder)
        {
            builder.Services.Configure<UmbracoPipelineOptions>(options =>
            {
                options.AddFilter(new UmbracoPipelineFilter(nameof(SitemapController))
                {
                    Endpoints = app => app.UseEndpoints(endpoints =>
                    {
                        endpoints.MapControllerRoute("Sitemap Index", "/sitemapindex.xml", new { Controller = "Sitemap", Action = "SitemapIndex" });
                    })
                });
            });
        }
    
    }
    

    The controller is being hit, and the FindContent method is being hit, but the Umbraco Context is always null.

  • Andreas Kristensen 65 posts 269 karma points c-trib
    Nov 24, 2021 @ 10:59
    Andreas Kristensen
    0

    I suspect this might not behave the way it's intended to. I have created an issue here: https://github.com/umbraco/Umbraco-CMS/issues/11692

  • Andreas Kristensen 65 posts 269 karma points c-trib
    Nov 25, 2021 @ 13:08
    Andreas Kristensen
    100

    This was caused by bad documentation. Explaination has been added to the documentation: https://github.com/umbraco/UmbracoDocs/pull/3662

  • Jay 409 posts 635 karma points
    Feb 15, 2022 @ 09:15
    Jay
    0

    Hey Andreas,

    I've tried to use the IUmbracoContextFactory instead but when I tried to call like umbracoContextReference?.UmbracoContext.Content.GetAtRoot()

    The GetAtRoot is always empty all the time. How do you get your root node using IUmbracoContextFactory inside FindContent?

  • Andreas Kristensen 65 posts 269 karma points c-trib
    Feb 15, 2022 @ 09:35
    Andreas Kristensen
    0

    Hi Jay,

    I just had a look at my code. This is how I did it. Does this look like yours? Also, are you implementing IVirtualPageController in your controller?

    using (UmbracoContextReference umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext())
                {
                    var rootNodes = umbracoContextReference.UmbracoContext.Content.GetAtRoot();
                }
    
  • Jay 409 posts 635 karma points
    Feb 15, 2022 @ 09:49
    Jay
    0

    Yup same code for the GetAtRoot()

    I've implemented IVirtualPageController (same like yours with the UmbracoPageController too)

    Weird

  • Andreas Kristensen 65 posts 269 karma points c-trib
    Feb 15, 2022 @ 09:53
    Andreas Kristensen
    0

    Hmm... I have actually refactored my code, and in that process, I went away from contextFactory to contextAccessor, because I copied some code from another controller. And it works for me, so maybe ContextFactory is not required nevertheless. Maybe something has changed in an Umbraco update, I dont know. My controller inherits from UmbracoPageController and implements IVirtualPageController, and it returns .xml. Here is how I get the root in FindContent:

    if (_umbracoContextAccessor.TryGetUmbracoContext(out IUmbracoContext umbracoContext))
                {
                    var root = CurrentPage.Root();
                }
    

    Maybe it has something to do with the inheritiance from UmbracoPageController. I am really not sure, I just know that it works.

  • Jay 409 posts 635 karma points
    Feb 15, 2022 @ 09:59
    Jay
    0

    oh ok let me try it out

    Which url are you hitting on your refactored code?

    Thanks

  • Andreas Kristensen 65 posts 269 karma points c-trib
    Feb 15, 2022 @ 10:02
    Andreas Kristensen
    0

    My controller is for a custom XML sitemap, so something like: /referencessitemap.xml.

  • Jay 409 posts 635 karma points
    Feb 15, 2022 @ 10:31
    Jay
    0

    Annoying, when it hit the CurrentPage.Root() bit it breaks and shows the below

    enter image description here

  • Andreas Kristensen 65 posts 269 karma points c-trib
    Feb 15, 2022 @ 11:31
    Andreas Kristensen
    0

    Are you sure that your custom routing is working? I used a composer like seen here, around halfway down the page, to register my custom routes. https://our.umbraco.com/documentation/reference/routing/custom-routes

  • Jay 409 posts 635 karma points
    Feb 15, 2022 @ 11:36
    Jay
    0

    yeah using your codes from your 1st comment. It hits into FindContent when I load the /sitemapindex.xml url

  • Jay 409 posts 635 karma points
    Feb 15, 2022 @ 13:20
    Jay
    0

    Oh Andreas,

    I know why, I've noticed that the site actually using variant so I need to pass in the culture when I call .GetAtRoot()

Please Sign in or register to post replies

Write your reply to:

Draft