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

Wai*_*ein 5 asp.net-mvc routes robots.txt asp.net-mvc-routing

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

这是我在HomeController中的动作方法

public ActionResult Robots()
{
    Response.ContentType = "text/plain";
    return View();
}
Run Code Online (Sandbox Code Playgroud)

这是我的机器人视图

@{
    Layout = null;
}

User-agent:*
Disallow:/
Run Code Online (Sandbox Code Playgroud)

我在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
}
Run Code Online (Sandbox Code Playgroud)

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

Nko*_*osi 8

默认情况下,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>
Run Code Online (Sandbox Code Playgroud)


Joh*_*aes 6

在 Asp.net Core 中,您只需将 robots.txt 文件添加到 wwwroot 目录即可。

在此处输入图片说明