我正在尝试为基于HttpRequest.UserAgent
.
我IHttpModule
在服务器的全局中配置了一个web.config
包含以下代码的:
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();
}
}
在浏览器中设置User-Agent
并访问页面会在事件日志中生成一个条目,但该页面也会返回,但没有 404 状态。
对我所缺少的有什么想法吗?
更新:
如果我将它添加到单个站点(在站点中),这IHttpModule
似乎确实有效,但web.config
不适用于所有站点。
更新 2:
仅适用于IIS7服务器IHttpModule
上的单个站点,而不适用于IIS6。