如何正确动态生成robots.txt ?



我有几个站点,它们都有相同的robots.txt。

当我修改一个时,我必须修改其他的。

我有一个想法,通过后端代码生成robots.txt。后端将通过WEB API从远程服务器获取数据并生成数据。

下面是StackOverflow教程中的代码:

public class OthersController: Controller
{
[Route("/robots.txt")]                
public ContentResult RotbotsTXT()
{
String Result=///some code get robots data from remote server.
return this.Content(Result, "text/plain", Encoding.UTF8);
}
}

尽管如此,它在浏览器上运行良好。

然而,我遇到了一个奇怪的情况。

一些蜘蛛正确访问路由,但无法检测到它(例如百度蜘蛛)。

而且,一些蜘蛛(例如Google bot)会奇怪地访问路由www.abc.com//robots.txt (robots.txt从不存储在这里),但不是www.abc.com/robots.txt。

通过创建TXT文件回到原来的方法后,所有问题都解决了。

我的代码有什么问题?我怎么解它?谢谢你。

[Route("/robots.txt")]改为[Route("robots.txt")]:

public class OthersController: Controller
{
[Route("robots.txt")]                
public ContentResult RobotsTXT()
{
String Result=///some code get robots data from remote server.
return this.Content(Result, "text/plain", Encoding.UTF8);
}
}

最新更新