我正在开发一个 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();
}
这是我的机器人 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>
关于asp.net-mvc - 无法在 asp.net mvc 中映射 robots.txt 的路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38811358/