I'm trying to generate a 404 response for certain requests on all sites on a server based on the HttpRequest.UserAgent
.
I've configured an IHttpModule
in the server's global web.config
containing the code below:
private void Application_BeginRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
if (isBot(context.Request.UserAgent))
{
System.Diagnostics.EventLog.WriteEntry(
"Application",
"\nBotRequestChecker -- request 404d\n" + "Url: " +
context.Request.Url + "\nUserAgent: " + context.Request.UserAgent,
EventLogEntryType.Information);
context.Response.StatusCode = 404;
context.Response.StatusDescription = "Not Found";
context.ApplicationInstance.CompleteRequest();
}
}
Setting the User-Agent
in a browser and visiting a page results in an entry in the event log, but the page is also returned, without a 404 status.
Any thoughts on what I'm missing?
Update:
The IHttpModule
does seem to work if I add it to a single site (in the site's web.config
), just not for all sites.
Update 2:
The IHttpModule
only works on a single site on an IIS7 server, not on IIS6.