微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net-mvc – 无法在asp.net mvc中映射robots.txt的路由

我正在开发一个asp.net mvc应用程序.我正在为我的应用程序创建robots.txt以防止机器人,因为我当前的站点收到了很多机器人请求.所以我找到了这个链接,Robots.txt file in MVC.NET 4来创建robots.txt.但是当我访问我的应用程序时,如此输入网址“www.domain.com/robots.txt”,它总是返回404页面.请参阅下面的代码.

这是我在HomeController中的动作方法

public ActionResult Robots()
{
    Response.ContentType = "text/plain";
    return View();
}

这是我的机器人视图

@{
    Layout = null;
}

User-agent:*
disallow:/

我在RouteConfig中为robots.txt配置了这样的路由

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

            //routes.MapMvcAttributeRoutes();
            routes.MapRoute(
               "Robots.txt","robots.txt",new { controller = "Home",action = "Robots" },new string[] { "AyarDirectory.Web.Controllers" }
               );

          //other routes
}

但是当我访问此网址“www.domain.com/robots.txt”时,它总是返回404页面.如何正确地将robots.txt添加到我的应用程序中?

解决方法

认情况下,ASP.NET MVC中不允许创建以文件扩展名结尾的路由.要解决此安全限制,您需要将以下内容添加到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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐