3

我正在尝试为基于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

4

1 回答 1

0

确保您已订阅模块IHttpModule.Init()实现中的 BeginRequest 事件。这些事件不会IHttpModule像在 Global.asax 中那样在实现中自动连接。

一开始我也错过了关于全局 web.config 的内容。

在 64 位服务器上,您需要确保在您需要支持的所有 ASP.NET 版本中更改 32 位和 64 位配置(取决于您的站点运行的位数) :

%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

如果您同时针对 IIS6 和 IIS7,则需要确保在<system.web>/<httpModules>IIS6 的<system.webServer>/<modules>元素和 IIS7 的元素中都引用了该模块。

于 2011-03-31T18:08:23.523 回答