Trying to Build SiteMap.XML from custom control

I am trying to Build a control to generate a site map, before any one points me to the docs on Umbraco I know you can do it that way, but we need the sitemap to be called SiteMap.XML for a specific reason.

So When I try an Inject IUmbracoHelperAccessor umbracoHelper and then do the following in my custom control var s = _umbracoHelperAccessor.TryGetUmbracoHelper(out var _umbracoHelper);

System.InvalidOperationException: ‘Wasn’t able to get an UmbracoContext’

Any help would be useful.

We are injecting IUmbracoHelperAccessor umbracoHelper as part of the constructor for the controler.

Fixed by Injecting _umbracoContextFactory = umbracoContextFactory and then

var umbracoContext = _umbracoContextFactory.EnsureUmbracoContext();
IEnumerable webpages = umbracoContext.UmbracoContext.Content.GetAtRoot();

Creating a custom controller to solve what sounds like a naming/routing issue might be overkill.

You could just rewrite with something like this in program.cs.

var rewriteOptions = new RewriteOptions();

rewriteOptions.AddRewrite("sitemap.xml", "sitemap", true);
app.UseRewriter(rewriteOptions);
1 Like

Thanks for the update. I remember that the next time I need to do this, it was better to be safe then sorry with the say I did it, It needed to be a fully formatted and qualified site map file. This way I had a bit more control.

Though MS recommends the highest upstream agent to handle your rewrites.. ie IIS native rewrites rather than asp.net core process and middleware when possible?

Though it is nice to have in the asp middleware when say using Kestrel locally… and only IIS in production/uat :slight_smile:

I shouldn’t imagine that sitemap.xml is being called frequently enough for perf to be meaningful concern here.

When it is, there’s nothing wrong with defining it in middleware first then replicating on the edge server, whatever that is.

For sure, though just noting generically, consciously choose the mechanism that suits your use, as I’ve inherited sites that have CloudFlare rewrites, native iis rewrite module rules in the web.config, iis web.config rewriteMaps,
.AddIISUrlRewrite(builder.Environment.ContentRootFileProvider, “App_Data/Rewrite.config”), .AddRewrites(), IRules, Umbraco tracked UrlRedirects ,SeoChecker redirects… ,virtualContentNodes, last chance content finders … sure there are others.

Just makes things difficult to debug if routing goes awry.. :frowning:

So if it’s as simple as sitemap.xml… that for local dev you aren’t even going to expose… I’d say that’s even more apt for your edge server to deal with :slight_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.