简体   繁体   中英

ASP.NET HttpHandler mapping to Robots.txt without access to IIS

Is there a simple way to map /robots.txt to a HttpHandler using only the web.config? I've tried all sorts of changes to the httpHandlers tag but none have made any difference. All the examples require modifications to IIS site properties, which I cannot access.

Is there a way using only the web.config to map:

<add verb="*" path="/robots.txt" type="Site.RobotsHandler" />

correctly?

I'm currently using .NET 3.5 and MVC2.

If you're using MVC, why not create a route to an Action and handle it there rather than creating another handler?

routes.MapRoute("robots.txt", "robots.txt", new { controller = "Home", action = "Robots" });

EDIT BASED ON COMMENT

routes.IgnoreRoute("{file}.txt", new { pathInfo = new RobotsIgnore() });

public class RobotsIgnore : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        return values[parameterName].ToString().ToLowerInvariant() != "robots.txt";
    }
}

Based off some of the comments on other answers I'm guessing you're using iis6? As far as I know it's not possible to alter iis6 handler mappings via web config. .txt files will never reach asp.net without changing default mappings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM