我有一个ASP.NET MVC 4 Web应用程序,可以从多个不同的域访问。该网站基于请求中的域完全本地化(概念类似于this question)。
我想要包含robots.txt文件,我想基于域本地化robots.txt文件,但我知道我在网站的文件系统中只能有一个物理“robots.txt”文本文件。目录
使用ASP.NET MVC框架在每个域上实现robots.txt文件的最简单/最好的方式(甚至可能)是什么,以便相同的站点安装为每个域提供内容,但机器人文件的内容是根据请求的域进行本地化的?
答案 0 :(得分:53)
这个过程相当简单:
ContentResult
并将ContentType
设置为"text/plain"
FilePathResult
如果你的机器人文件只是磁盘上的文件,可以通过Controller
类上的一种辅助方法,例如File(name, "text/plain")
以下示例假定使用单个顶级robots.txt文件:
// In App_Start/RouteConfig:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "robots",
url: "robots.txt",
defaults: new { controller = "Seo", action = "Robots" }
);
// The controller:
public class SeoController : Controller {
public ActionResult Robots() {
var robotsFile = "~/robots-default.txt";
switch (Request.Url.Host.ToLower()) {
case "stackoverflow.com":
robotsFile = "~/robots-so.txt";
break;
case "meta.stackoverflow.com":
robotsFile = "~/robots-meta.txt";
break;
}
return File(robotsFile, "text/plain");
}
}
最简单的方法之一就是确保在web.config中使用runAllManagedModulesForAllRequests
为所有请求调用路由模块(不要使用它,请参阅下一段):< / p>
<system.webServer>
<handlers>
...
</handlers>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
这通常不是一件好事因为现在所有静态文件(css,js,txt)在转移到静态文件处理程序之前都会通过托管处理程序。 IIS 非常擅长快速提供静态文件(一个很大程度上静态的文件网站将在CPU之前最大化你的磁盘I / O方式),所以为了避免这种性能影响,推荐的方法就像网络一样。配置示例部分如下。请注意与Visual Studio MVC 4模板应用程序中ExtensionlessUrlHandler-Integrated-4.0
处理程序的相似性:
<system.webServer>
<handlers>
<add name="Robots-Integrated-4.0"
path="/robots.txt" verb="GET"
type="System.Web.Handlers.TransferRequestHandler"
preCondition="integratedMode,runtimeVersionv4.0" />
... the original handlers ...
</handlers>
<modules runAllManagedModulesForAllRequests="false" />
</system.webServer>
一旦开始使用,这种方法的优势就会变得明显:
缺点是,
还要记住,不同的robots.txt文件可用于不同的子目录。使用路由和控制器方法会变得棘手,因此IHttpHandler
方法(下面)对于这种情况更容易。
您也可以使用自定义IHttpHandler
registered in your web.config执行此操作。我强调自定义,因为这样可以避免让所有控制器看到所有请求(使用runAllManagedModulesForAllRequests="true"
,而不像在路由表中添加自定义路由处理程序。
这也可能是比控制器更轻量级的方法,但您必须拥有巨大的网站流量才能注意到差异。它的另一个好处是可以在您的所有站点上使用的可重复使用的代码段。您还可以添加自定义配置部分,以配置机械手文件的机械手用户代理/域名/路径映射。
<system.webServer>
<handlers>
<add name="Robots" verb="*" path="/robots.txt"
type="MyProject.RobotsHandler, MyAssembly"
preCondition="managedHandler"/>
</handlers>
<modules runAllManagedModulesForAllRequests="false" />
</system.webServer>
public class RobotsHandler: IHttpHandler
{
public bool IsReusable { get { return false; } }
public void ProcessRequest(HttpContext context) {
string domain = context.Request.Url.Host;
// set the response code, content type and appropriate robots file here
// also think about handling caching, sending error codes etc.
context.Response.StatusCode = 200;
context.Response.ContentType = "text/plain";
// return the robots content
context.Response.Write("my robots content");
}
}
子目录中的要为子目录和站点根目录提供机器人,您无法轻松使用控制器方法;在这种情况下,处理程序方法更简单。这可以配置为将robots.txt文件请求提取到任何子目录并相应地处理它们。然后,您可以选择为某些目录返回404,或为其他目录返回robots文件的子部分。
我在这里特别提到这一点,因为这种方法也可以用于sitemap.xml文件,为站点的不同部分提供不同的站点地图,提供相互引用的多个站点地图等。
其他参考文献: