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 29 posts 199 karma points c-trib
    1 week ago
    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 29 posts 199 karma points c-trib
    1 week ago
    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 29 posts 199 karma points c-trib
    1 week ago
    Andreas Kristensen
    100

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

Please Sign in or register to post replies

Write your reply to:

Draft