我有一个robots.txt,它不是静态的,而是动态生成的.我的问题是创建从root / robots.txt到我的控制器操作的路由.
这有效:
routes.MapRoute( name: "Robots",url: "robots",defaults: new { controller = "Home",action = "Robots" });
这不起作用:
routes.MapRoute( name: "Robots",url: "robots.txt",/* this is the only thing I've changed */ defaults: new { controller = "Home",action = "Robots" });
“.txt”显然导致ASP到barf
解决方法
您需要将以下内容添加到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>
有关详细信息,请参阅我在Dynamically Generating Robots.txt Using ASP.NET MVC上发表的博文.