Copied to clipboard

Flag this post as spam?

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


  • Chris Goehrs 2 posts 32 karma points
    Jul 23, 2024 @ 16:20
    Chris Goehrs
    0

    Controllers with ".xml" in the route template - Umbraco 14

    I'm running into an issue where I'd like to provide XML sitemap using a controller. The sitemap may or may not include paginated results so keeping it dynamically produced within a number of controller endpoints is ideal. While I am trying to use SimpleMvcSitemp.Core to handle this, I'm running into one issue.

    When my route template ends in .xml, the data from any documents doesn't seem to load so, for instance, getting the urls is impossible and always returns "#". But if I remove the .xml portion, everything works as intended.

    SO:

    template: [Route("foobar")] works

    template: [Route("foobar.xml")] doesn't work

    What am I missing? Is there any way I can get this to work and keep this in an external controller?

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Jul 24, 2024 @ 08:34
    Huw Reddick
    100

    You could use some urlrewrite middleware like below maybe

    public class RewriteRules
    {  
        public static void ReWriteRequests(RewriteContext context)
        {
            var request = context.HttpContext.Request;
    
            if (request.Path.Value.EndsWith("foobar.xml", StringComparison.OrdinalIgnoreCase))
            {
                context.HttpContext.Request.Path = context.HttpContext.Request.Path.Value.Replace(".xml", "");
    
            }
        }
    }
    

    register in your startup.cs

    app.UseRewriter(new RewriteOptions()
                        .Add(RewriteRules.ReWriteRequests)
                        );
    
  • Chris Goehrs 2 posts 32 karma points
    Jul 31, 2024 @ 18:05
    Chris Goehrs
    0

    Thanks Huw,

    This was a great idea for a workaround.

    My question still stands regarding the necessity of this. Why is this an issue in the first place?

    Even so, this is a simple workaround that works well. Thank you!

Please Sign in or register to post replies

Write your reply to:

Draft