Static file routes in ASP.NET MVC

I am working on an ASP.NET MVC application. In this application, I need to dynamically generate a sitemap when it is requested. I know how to set up routes in general. However, I'm not sure if I can create a route for a specific file. I currently have the following in RouteConfig.cs:

routes.MapRoute(
  name: "Sitemap",
  url: "resources/sitemap.xml",
  defaults: new { controller = "Site", action = "Sitemap" }
);

In my SiteController, I have the following:

public ActionResult Sitemap()
{
  // I will build my sitemap.xml file here and return it.
}

When I enter /resources/sitmap.xml in the address bar of the browser, I notice that my Sitemap () action never fires. Is it even possible in ASP.NET MVC to configure a route for a specific file? If so, how?

Thanks,

+4
source share
1 answer

So you need to take a few steps -

1 -. xml, .Net Web.config <system.webServer> -

<handlers>
  <add name="HtmlFileHandler" path="*.xml" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>

2 - , .

routes.RouteExistingFiles = true;

routes.MapRoute(
   name: "Sitemap",
   url: "{site}.xml",
   defaults: new { controller = "Site", action = "Sitemap", site = UrlParameter.Optional }
 );

3. /SiteMap.xml, Controller.

+5

Source: https://habr.com/ru/post/1528088/


All Articles