简体   繁体   中英

How to Route to Url *with* Extension

I'm trying to deliver my blog's sitemap.xml file generated by a Razor view, like so in my _AppStart.cshtml file:

//sitemap
RouteTable.Routes.MapWebPageRoute("sitemap.xml", "~/pages/shared/sitemap.cshtml");

This route is ignored for some reason, and I get a 404. It works fine if I route it to "/sitemap", but the moment I include the file extension it breaks. I'm assuming IIS is doing something with the request before ASP.NET gets hold of it, but I"m not sure what to do about it.

Try making the extension a parameter:

RouteTable.Routes.MapWebPageRoute(
    "sitemap.{extension}", // route pattern
    "~/pages/shared/sitemap.cshtml", // physical file
    defaultValues: new {extension = "xml"}, // defaults
    constraints: new {extension = "xml"}); // constraints (regex)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM