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.
source share