MVC:如何将/sitemap.xml路由到ActionResult?

时间:2010-01-05 14:18:11

标签: asp.net-mvc seo sitemap sitemap.xml

我有一个SitemapActionResult会覆盖ActionResult,并在http://www.sprelle.no/Home/SiteMap被点击时提供SEO sitemap.xml。到目前为止一切都很好。

但我想要的是在Google访问/sitemap.xml时提供sitemap.xml。为了实现这一目标,我需要一个看到“sitemap.xml”并指向/ Home / Sitemap的路线。

如何创建此映射(在Routes表中)?

3 个答案:

答案 0 :(得分:18)

添加以下地图:

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

请注意路由,控制器和操作选项是硬编码的。

答案 1 :(得分:7)

你可以用它。

步骤1.将文件扩展名映射到TransferRequestHandler

IIS 7集成模式使用HTTP处理程序映射,将路径/动词组合指向HTTP处理程序。例如,有一个默认的处理程序映射,它指向path =" * .axd"动词=" GET,HEAD,POST,DEBUG"到运行该站点的.NET运行时版本的相应ISAPI模块。在IIS Express下查看默认处理程序的最简单方法是在IIS Express下运行站点,右键单击系统托盘中的IIS Express图标,单击"显示所有应用程序",然后单击站点。底部的applicationhost.config链接是链接的,因此您只需单击它就可以在Visual Studio中加载。

如果您滚动到底部,则会看到path="*" verb="*"的{​​{1}}的一个静态文件映射指向StaticFileModule,DefaultDocumentModule,DirectoryListingModule。如果你什么也不做的话,那会处理你的.html请求。因此,第一步是在web.config中添加一个处理程序,该处理程序将*.html请求指向TransferRequestHandlerTransferRequestHandler是处理您曾经在MVC路由中看到的无扩展名网址的处理程序,例如/store/details/5

添加处理程序映射非常简单 - 只需打开web.config并将其添加到<system.webServer/handlers>节点即可。

<add name="HtmlFileHandler" path="*.html" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

请注意,如果您愿意,可以使路径更具体。例如,如果您只想拦截一个特定请求,则可以使用path =&#34; sample.html&#34;

步骤2.配置路线

接下来,您需要一条新路线。打开App_Start/RouteConfig.cs,然后打开RegisterRoutes电话。我的完整RegisterRoutes看起来像这样:

  routes.MapRoute(
       name: "XMLPath",
       url: "sitemapindex.xml",
       defaults: new { controller = "Home", action = "Html", page = UrlParameter.Optional }
   );

步骤3.路由现有文件

这几乎涵盖了它,但还有一件事需要处理 - 覆盖与现有文件匹配的请求。如果您有一个名为myfile.html的实际文件,则路由系统不会允许您的路由运行。我忘记了这一点,最终出现了HTTP 500错误(递归溢出),不得不向Eilon Lipton寻求帮助。

无论如何,这很容易解决 - 只需在路线注册中添加routes.RouteExistingFiles = true即可。我完成的RegisterRoutes调用如下所示:

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.RouteExistingFiles = true;

        routes.MapRoute(
            name: "CrazyPants",
            url: "{page}.html",
            defaults: new { controller = "Home", action = "Html", page = UrlParameter.Optional }
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
    }

那就是它。

我通过添加此控制器操作进行了测试:

public FileResult Html()
{
    var stringBuilder = new StringBuilder();
    stringBuilder.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    stringBuilder.AppendLine("<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">");
    stringBuilder.AppendLine("<sitemap>");
    stringBuilder.AppendLine("<loc>http://sprint-newhomes.move.com/sitemaps/sitemap_01.xml</loc>");
    stringBuilder.AppendLine("<lastmod>" + DateTime.Now.ToString("MMMM-dd-yyyy HH:mm:ss tt") + "</lastmod>");
    stringBuilder.AppendLine("</sitemap>");
    stringBuilder.AppendLine("<sitemap>");
    stringBuilder.AppendLine("<loc>http://sprint-newhomes.move.com/sitemaps/sitemap_02.xml</loc>");
    stringBuilder.AppendLine("<lastmod>" + DateTime.Now.ToString("MMMM-dd-yyyy HH:mm:ss tt") + "</lastmod>");
    stringBuilder.AppendLine("</sitemap>");
    stringBuilder.AppendLine("</sitemapindex>");

    var ms = new MemoryStream(Encoding.ASCII.GetBytes(stringBuilder.ToString()));



    Response.AppendHeader("Content-Disposition", "inline;filename=sitemapindex.xml");
    return new FileStreamResult(ms, "text/xml");
}

答案 2 :(得分:2)

要实现这一点,您需要做两件事:

  1. 指示IIS允许静态文件请求&#34; /sitemap.xml &#34;打你的控制器。否则,IIS将绕过您的应用程序并直接查找具有此名称的文件。将以下行添加到web.config:
  2. <system.webServer>
        <handlers>
    
            <!-- add the following line -->
            <add name="SitemapXml" path="sitemap.xml" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/>
    
        </handlers>
    </system.webServer>
    
    1. 在MVC应用程序中放置一条与此请求匹配的路径到ActionResult(确保将其放在默认路由之前):
    2. routes.MapRoute(
          name: "Sitemap",
          url: "sitemap.xml",
          defaults: new { controller = "YourControllerName", action = "YourActionName" }
      );
      
相关问题