如何在ASP.NET MVC中添加路由到动态robots.txt?

JSS*_*JSS 7 asp.net-mvc robots.txt asp.net-mvc-routing

我有一个robots.txt,它不是静态的,而是动态生成的.我的问题是创建从root/robots.txt到我的控制器操作的路由.

有效:

routes.MapRoute(
name: "Robots",
url: "robots",
defaults: new { controller = "Home", action = "Robots" });
Run Code Online (Sandbox Code Playgroud)

不起作用:

routes.MapRoute(
name: "Robots",
 url: "robots.txt", /* this is the only thing I've changed */
defaults: new { controller = "Home", action = "Robots" });
Run Code Online (Sandbox Code Playgroud)

".txt"显然导致ASP到barf

Muh*_*eed 13

您需要将以下内容添加到web.config文件中,以允许执行具有文件扩展名的路由.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <!-- ...Omitted -->
  <system.webServer>
    <!-- ...Omitted -->
    <handlers>
      <!-- ...Omitted -->
      <add name="RobotsText" 
           path="robots.txt" 
           verb="GET" 
           type="System.Web.Handlers.TransferRequestHandler" 
           preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅我的博客文章,动态生成Robots.txt使用ASP.NET MVC.


JSS*_*JSS 6

在这里回答:扩展名没有通过路由处理的网址.基本上,当asp看到"." 它调用静态文件处理程序,因此永远不会使用动态路由.需要修改web.config文件,以便静态文件处理程序不会拦截/robots.txt.