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 cod开发者_如何学Pythone 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.
Make sure that you've subscribed to the the BeginRequest event in your module's IHttpModule.Init() implementation. The events don't get auto-wired in IHttpModule implementations the way same they do in Global.asax.
I also missed the bit about the global web.config at first.
On a 64-bit server, you'll need to make sure you make the changes in both the 32- and 64-bit configurations (depending on the bitness that your sites are running in) in all ASP.NET versions you need to support:
%windows%\Microsoft.NET\Framework\v2.0.50727\CONFIG\web.config
%windows%\Microsoft.NET\Framework64\v2.0.50727\CONFIG\web.config
%windows%\Microsoft.NET\Framework\v4.0.30319\CONFIG\web.config
%windows%\Microsoft.NET\Framework64\v4.0.30319\CONFIG\web.config
If you're targeting both IIS6 and IIS7, you'll need to make sure the module is referenced in both the <system.web>/<httpModules> element for IIS6 and the <system.webServer>/<modules> element for IIS7.
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论