【问题标题】:How to add route to dynamic robots.txt in ASP.NET MVC?如何在 ASP.NET MVC 中添加到动态 robots.txt 的路由?
【发布时间】:2013-06-14 04:30:02
【问题描述】:

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

有效

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

不起作用

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

“.txt”显然会导致 ASP 出错

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-routing robots.txt


    【解决方案1】:

    在这里回答:url with extension not getting handled by routing。基本上,当 asp 看到“。”时。它调用静态文件处理程序,因此从不使用动态路由。需要修改 web.config 文件,以便 /robots.txt 不会被静态文件处理程序拦截。

    【讨论】:

      【解决方案2】:

      您需要将以下内容添加到您的 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>
      

      有关详细信息,请参阅我在 Dynamically Generating Robots.txt Using ASP.NET MVC 上的博客文章。

      【讨论】:

        【解决方案3】:

        由于我工作的环境,Muhammad Rehan Saeed 在 web.config 方法中的 System.Web.Handlers.TransferRequestHandler 方法对我不起作用,导致 500 错误。

        使用 web.config url 重写规则的替代方法对我有用:

        <rewrite>
            <rules>
                <rule name="Dynamic robots.txt" stopProcessing="true">
                    <match url="robots.txt" />
                    <action type="Rewrite" url="/DynamicFiles/RobotsTxt" />
                </rule>
            </rules>
        </rewrite>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-24
          • 2016-12-13
          • 2011-06-18
          • 2012-07-23
          • 2017-01-22
          • 2011-02-01
          相关资源
          最近更新 更多