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

二次信任 提交于 2019-11-30 18:04:10

问题


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

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

I have tried using

RouteTable.Routes.Add("SitemapRoute", new Route("sitemap.xml", new PageRouteHandler("~/sitemap.aspx")))

回答1:


Your code is correct, and should be placed in the Application_Start method in Global.asax. For example:

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 that *.xml files are handled by ASP.NET. By default, *.xml files will just be served up by IIS and not processed by ASP.NET. To make sure they are processed by ASP.NET, you can either:

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

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

or 2) add a "Handler Mapping" for .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 a sample ASP.NET (non-MVC) project and was able to get the routing to work as you specified.



来源:https://stackoverflow.com/questions/8845812/cant-figure-out-how-to-route-my-non-mvc-site-from-sitemap-xml-to-another-aspx-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!