Rob
Rob

Reputation: 1777

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.

Upvotes: 0

Views: 1624

Answers (2)

Kenneth Ito
Kenneth Ito

Reputation: 5261

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.

Upvotes: 0

Anthony Shaw
Anthony Shaw

Reputation: 8166

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";
    }
}

Upvotes: 1

Related Questions