I am developing an asp.net mvc application. I am creating robots.txt for my application to prevent from bots because my current site is getting many robot requests. So I found this link, Robots.txt file in MVC.NET 4 to create robots.txt. But I when I access my application like this entering url, "www.domain.com/robots.txt", it is always returning 404 page. Please see my code below.
我正在开发一个asp.net mvc应用程序。我正在为我的应用程序创建robots.txt以防止机器人,因为我当前的站点收到了很多机器人请求。所以我在MVC.NET 4中找到了这个链接,Robots.txt文件来创建robots.txt。但是当我访问我的应用程序时,如此输入网址“www.domain.com/robots.txt”,它总是返回404页面。请参阅下面的代码。
This is my action method in HomeController
这是我在HomeController中的动作方法
public ActionResult Robots()
{
Response.ContentType = "text/plain";
return View();
}
This is my robots view
这是我的机器人视图
@{
Layout = null;
}
User-agent:*
Disallow:/
I configured route for robots.txt like this in RouteConfig
我在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
}
But when I access this url, "www.domain.com/robots.txt", it is always returning 404 page. How can I add robots.txt correctly to my application?
但是,当我访问此网址“www.domain.com/robots.txt”时,它总是返回404页面。如何正确地将robots.txt添加到我的应用程序中?
2 个解决方案
#1
4
Creating a route ending with a file extension is not allowed by default in ASP.NET MVC. To get around this security restriction, you need to add the following to the Web.config file:
默认情况下,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>
#2
1
Would say same thing as above you need to change the web.config to allow the route with .txt to work. I had same issue with a project I was working on and I got it to work.
如上所述,您需要更改web.config以允许使用.txt的路由。我和我正在进行的项目有同样的问题,我让它开始工作。
However if you using a view for the output of the robots without a model you might as well keep a static robots.txt as it will give you no advantage. Another way is to output the text direct from the action using a string builder.
但是,如果您在没有模型的情况下使用视图输出机器人,那么您也可以保留静态robots.txt,因为它不会给您带来任何好处。另一种方法是使用字符串构建器直接从操作输出文本。
#1
4
Creating a route ending with a file extension is not allowed by default in ASP.NET MVC. To get around this security restriction, you need to add the following to the Web.config file:
默认情况下,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>
#2
1
Would say same thing as above you need to change the web.config to allow the route with .txt to work. I had same issue with a project I was working on and I got it to work.
如上所述,您需要更改web.config以允许使用.txt的路由。我和我正在进行的项目有同样的问题,我让它开始工作。
However if you using a view for the output of the robots without a model you might as well keep a static robots.txt as it will give you no advantage. Another way is to output the text direct from the action using a string builder.
但是,如果您在没有模型的情况下使用视图输出机器人,那么您也可以保留静态robots.txt,因为它不会给您带来任何好处。另一种方法是使用字符串构建器直接从操作输出文本。