如何从全局 web.config 中配置的 IHttpModule 生成 404 响应?
我正在尝试根据服务器上所有站点上的某些请求生成 404 响应 HttpRequest.UserAgent
。
我已经在服务器的全局 web.config
中配置了一个 IHttpModule
,其中包含以下代码:
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
中),IHttpModule
似乎确实可以工作,但不适用于所有人网站。
更新2: IHttpModule
仅适用于 IIS7 服务器上的单个站点,不适用于 IIS6。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保您已订阅模块的
IHttpModule.Init()
实现中的 BeginRequest 事件。这些事件在IHttpModule
实现中不会像在 Global.asax 中那样自动连接。我一开始也错过了有关全局 web.config 的部分。
在 64 位服务器上,您需要确保在需要支持的所有 ASP.NET 版本中对 32 位和 64 位配置(取决于您的站点运行的位数)进行更改:
如果您同时针对 IIS6 和 IIS7,则需要确保在
/
元素中引用该模块IIS6 和 IIS7 的/
元素。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 inIHttpModule
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:
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.