相关疑难解决方法(0)

如何在ASP.NET MVC中添加路由到动态robots.txt?

我有一个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 asp.net-mvc-routing

7
推荐指数
2
解决办法
5210
查看次数

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

我正在开发一个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添加到我的应用程序中?

asp.net-mvc routes robots.txt asp.net-mvc-routing

5
推荐指数
2
解决办法
1639
查看次数