【问题标题】:Cant figure out how to route my NON-MVC site from sitemap.xml to another .aspx page无法弄清楚如何将我的非 MVC 站点从 sitemap.xml 路由到另一个 .aspx 页面
【发布时间】:2012-01-13 04:09:05
【问题描述】:

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

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

我尝试过使用

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

【问题讨论】:

    标签: asp.net routing routes sitemap.xml


    【解决方案1】:

    您的代码是正确的,应该放在Global.asax 中的Application_Start 方法中。例如:

    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) 在web.config 中的system.webServer 元素上指定runAllManagedModulesForAllRequests="true"

    <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)项目中对此进行了测试,并且能够让路由按照您指定的方式工作。

    【讨论】:

    • 哇,非常感谢。如果你住在加州亨廷顿海滩附近,我请你喝一杯。
    • 如果你在北卡罗来纳州夏洛特附近的任何地方 - 饮料都在我身上!
    猜你喜欢
    • 2020-12-19
    • 2016-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多