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:
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.
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 theFindContent
method.Here is my controller, implementing IVirtualPageController:
Here is the composer where I register the new route:
The controller is being hit, and the
FindContent
method is being hit, but the Umbraco Context is always null.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
This was caused by bad documentation. Explaination has been added to the documentation: https://github.com/umbraco/UmbracoDocs/pull/3662
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?
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?
Yup same code for the GetAtRoot()
I've implemented IVirtualPageController (same like yours with the UmbracoPageController too)
Weird
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 implementsIVirtualPageController
, and it returns .xml. Here is how I get the root in FindContent:Maybe it has something to do with the inheritiance from
UmbracoPageController
. I am really not sure, I just know that it works.oh ok let me try it out
Which url are you hitting on your refactored code?
Thanks
My controller is for a custom XML sitemap, so something like: /referencessitemap.xml.
Annoying, when it hit the CurrentPage.Root() bit it breaks and shows the below
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
yeah using your codes from your 1st comment. It hits into FindContent when I load the /sitemapindex.xml url
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()
is working on a reply...