Cant figure out how to redirect my NON-MVC site from sitemap.xml to another .aspx page

When searching on Google, the only solutions for this are for MVC websites. My asp.net 4.0 site is not MVC. I want sitemap.xml requests to load another dynamic .aspx page so that I can generate google links on the fly.

I spent hours searching, please, if you know where I can find the answer, let me know.

I tried using

RouteTable.Routes.Add("SitemapRoute", new Route("sitemap.xml", new PageRouteHandler("~/sitemap.aspx"))) 
+4
source share
1 answer

Your code is correct and must be placed in the Application_Start method in Global.asax . For instance:

 void Application_Start(object sender, EventArgs e) { RouteTable.Routes.Add(new Route( "sitemap.xml", new PageRouteHandler("~/sitemap.aspx"))); } 

However, you also need to make sure * .xml files are processed by ASP.NET. By default, * .xml files will only be served by IIS and will not be processed by ASP.NET. To make sure that they are handled by ASP.NET, you can:

1) Specify runAllManagedModulesForAllRequests="true" in the system.webServer element in web.config :

 <system.webServer> <modules runAllManagedModulesForAllRequests="true"> </modules> </system.webServer> 

or 2) add a "Mapping handler" for the .xml files:

 <system.webServer> <handlers> <add name="xml-file-handler" path="*.xml" type="System.Web.UI.PageHandlerFactory" verb="*" /> </handlers> </system.webServer> 

I tested this in an example ASP.NET project (not MVC) and was able to make routing work as you indicated.

+6
source

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


All Articles