【问题标题】:transfer sitemap.xml http request to route handler in ASP.NET MVC将 sitemap.xml http 请求传输到 ASP.NET MVC 中的路由处理程序
【发布时间】:2011-02-07 13:32:11
【问题描述】:

我正在尝试添加一个路由,它将所有 sitemap.xml 请求传输到我创建的自定义请求处理程序。

我尝试使用以下代码:

        routes.Add(new Route("sitemap.xml", new Helpers.SiteMapRouteHandler()));
        routes.MapRoute(
            "Default",                                              
            "{controller}/{action}/{id}",                           
            new { controller = "Home", action = "Index", id = "" }  
        );

但是当我使用Url.Action() 建立链接时:

Url.Action("Index", new { controller = "About"})

当我尝试导航到 XML 文件时,我得到以下信息:

/sitemap.xml?action=Index&controller=About

我做错了什么?

回答:

我使用了这个解决方案:

Specifying exact path for my ASP.NET Http Handler

【问题讨论】:

  • 您的路线列表中的哪个位置?它看起来很通用,因此它将匹配所有请求。您可能想使用 Phil Haack 的 Route Debugger 来帮助您。

标签: asp.net-mvc asp.net-mvc-2 asp.net-mvc-routing


【解决方案1】:

如果您想路由并操作而不是请求处理程序

您可以添加这样的路线

routes.MapRoute(
        "Sitemap",
        "sitemap.xml",
        new { controller = "Home", action = "SiteMap" }
        );

正如这里提到的 - MVC: How to route /sitemap.xml to an ActionResult? 它对我有用

更新:还要确保设置了<modules runAllManagedModulesForAllRequests="true">

【讨论】:

    【解决方案2】:

    我不确定这是否能解决问题,但值得一试:

    routes.Add(
        new Route(
            "{request}",
            new RouteValueDictionary(new { id = "" }),  // this might be the tricky part to change
            new RouteValueDictionary(new { request = "sitemap.xml" }),
            new Helpers.SiteMapRouteHandler()
        )); 
    

    【讨论】:

    • 不,它不起作用。尝试获取 /sitemap.xml 时收到 HTTP 404
    • 希望我能得到更多帮助。我能想到的唯一另一件事是尝试将两个 RVD 都设置为 (new { request = "sitemap.xml" })
    【解决方案3】:

    您必须在 web.config 文件中添加一个处理程序。

    <add name="SitemapFileHandler" path="sitemap.xml" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/>
    

    并配置您的路线:

    routes.RouteExistingFiles = true;
    

    在这里阅读更多: http://weblogs.asp.net/jongalloway//asp-net-mvc-routing-intercepting-file-requests-like-index-html-and-what-it-teaches-about-how-routing-works

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-12
      • 1970-01-01
      • 2011-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多