我有一个robots.txt,它不是静态的,而是动态生成的.我的问题是创建从root/robots.txt到我的控制器操作的路由.
这有效:
routes.MapRoute(
name: "Robots",
url: "robots",
defaults: new { controller = "Home", action = "Robots" });
Run Code Online (Sandbox Code Playgroud)
这不起作用:
routes.MapRoute(
name: "Robots",
url: "robots.txt", /* this is the only thing I've changed */
defaults: new { controller = "Home", action = "Robots" });
Run Code Online (Sandbox Code Playgroud)
".txt"显然导致ASP到barf
我正在开发一个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添加到我的应用程序中?