如何在ASP.NET MVC中添加到动态robots.txt的路由? [英] How to add route to dynamic robots.txt in ASP.NET MVC?

查看:85
本文介绍了如何在ASP.NET MVC中添加到动态robots.txt的路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a robots.txt that is not static but generated dynamically. My problem is creating a route from root/robots.txt to my controller action.

有效:

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明显发声

The ".txt" causes ASP to barf apparently

推荐答案

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

You need to add the following to your web.config file to allow the route with a file extension to execute.

<?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>

请参阅使用以下方法动态生成Robots.txt的博客文章:有关更多详细信息,请访问ASP.NET MVC .

这篇关于如何在ASP.NET MVC中添加到动态robots.txt的路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆