ama*_*eur 22 seo robots.txt asp.net-mvc-4
我有一个ASP.NET MVC 4 Web应用程序,可以从多个不同的域访问.该站点基于请求中的域完全本地化(类似于此问题的概念).
我想要包含robots.txt文件,我想基于域本地化robots.txt文件,但我知道我只能在站点的文件系统目录中有一个物理"robots.txt"文本文件.
使用ASP.NET MVC框架在每个域上实现robots.txt文件的最简单/最好的方式(甚至可能)是什么,以便相同的站点安装为每个域提供内容,但内容机器人文件的位置取决于所请求的域?
And*_*own 54
这个过程相当简单:
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");
}
}
Run Code Online (Sandbox Code Playgroud)
然后,最简单的方法之一就是确保为runAllManagedModulesForAllRequestsweb.config中使用的所有请求调用路由模块(不要使用它,请参阅下一段):
<system.webServer>
<handlers>
...
</handlers>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
这通常不是一件好事,因为现在所有静态文件(css,js,txt)在转移到静态文件处理程序之前都会通过托管处理程序.IIS 非常擅长快速提供静态文件(一个很大程度上静态的文件网站会在CPU之前最大化你的磁盘I/O方式),所以为了避免这种性能,建议的方法就像下面的web.config示例部分一样.请注意ExtensionlessUrlHandler-Integrated-4.0Visual Studio MVC 4模板应用程序中与处理程序的相似性:
<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>
Run Code Online (Sandbox Code Playgroud)
一旦开始使用,这种方法的优点就变得很明显:
在不利方面,
还要记住,不同的robots.txt文件可用于不同的子目录.使用路径和控制器方法会变得棘手,因此这种IHttpHandler方法(下面)更容易.
您也可以使用IHttpHandler 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>
Run Code Online (Sandbox Code Playgroud)
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");
}
}
Run Code Online (Sandbox Code Playgroud)
要为子目录和站点根目录提供机器人,您无法轻松使用控制器方法; 在这种情况下,处理程序方法更简单.这可以配置为将robots.txt文件请求提取到任何子目录并相应地处理它们.然后,您可以选择为某些目录返回404,或为其他目录返回robots文件的子部分.
我在这里特别提到这一点,因为这种方法也可以用于sitemap.xml文件,为站点的不同部分提供不同的站点地图,提供相互引用的多个站点地图等.
其他参考文献:
| 归档时间: |
|
| 查看次数: |
7755 次 |
| 最近记录: |