4

在搜索谷歌时,唯一的解决方案是针对 MVC 网站。我的 asp.net 4.0 站点不是 MVC。我希望对 sitemap.xml 的请求能够加载另一个动态 .aspx 页面,这样我就可以即时为 google 生成链接。

我花了几个小时搜索,如果你知道我在哪里可以找到答案,请告诉我。

我试过使用

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

1 回答 1

6

你的代码是正确的,应该Application_Start放在Global.asax. 例如:

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

但是,您还需要确保 *.xml 文件由 ASP.NET 处理。默认情况下,*.xml 文件将仅由 IIS 提供,而不由 ASP.NET 处理。要确保它们由 ASP.NET 处理,您可以:

1)在您runAllManagedModulesForAllRequests="true"system.webServer元素上指定web.config

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

或 2) 为 .xml 文件添加“处理程序映射”:

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

我在一个示例 ASP.NET(非 MVC)项目中对此进行了测试,并且能够使路由按您指定的方式工作。

于 2012-01-13T06:06:57.847 回答