用于集成 IIS 7 的自定义 HttpModule
我在构建自定义错误处理程序时遇到了麻烦。它应该是一个 HttpModule
,但是当我将其添加到 web.config
的 system.webServer/modules
标记时,它没有启动。
这是我的 web.config
部分:
<system.webServer>
<modules>
<add name="AspExceptionHandler"
type="Company.Exceptions.AspExceptionHandler, Company.Exceptions"
preCondition="managedHandler" />
</modules>
</system.webServer>
这是我的 HttpModule
中的代码:
using System;
using System.Web;
using Company.Settings;
using System.Configuration;
namespace Company.Exceptions
{
public class AspExceptionHandler : IHttpModule
{
public void Dispose() { }
public void Init(HttpApplication application)
{
application.Error += new EventHandler(ErrorHandler);
}
private void ErrorHandler(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext currentContext = application.Context;
// Gather information5
Exception currentException = application.Server.GetLastError();
String errorPage = "http://www.mycompaniesmainsite.com/error.html";
HttpException httpException = currentException as HttpException;
if (httpException == null || httpException.GetHttpCode() != 404)
{
currentContext.Server.Transfer(errorPage, true);
}
else
{
application.Response.Status = "404 Not Found";
application.Response.StatusCode = 404;
application.Response.StatusDescription = "Not Found";
currentContext.Server.Transfer(errorPage, true);
}
}
}
}
有人可以向我解释一下我做错了什么,以及 IIS 7 集成托管管道模式如何作品?因为我找到的大多数答案都是关于为 IIS 6 配置 HttpModules
的。
I'm having troubles with a custom Error handler I built. It should be a HttpModule
, but when I add it to my web.config
's system.webServer/modules
tag, it is not initiated.
This is my web.config
section:
<system.webServer>
<modules>
<add name="AspExceptionHandler"
type="Company.Exceptions.AspExceptionHandler, Company.Exceptions"
preCondition="managedHandler" />
</modules>
</system.webServer>
This is the code in my HttpModule
:
using System;
using System.Web;
using Company.Settings;
using System.Configuration;
namespace Company.Exceptions
{
public class AspExceptionHandler : IHttpModule
{
public void Dispose() { }
public void Init(HttpApplication application)
{
application.Error += new EventHandler(ErrorHandler);
}
private void ErrorHandler(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext currentContext = application.Context;
// Gather information5
Exception currentException = application.Server.GetLastError();
String errorPage = "http://www.mycompaniesmainsite.com/error.html";
HttpException httpException = currentException as HttpException;
if (httpException == null || httpException.GetHttpCode() != 404)
{
currentContext.Server.Transfer(errorPage, true);
}
else
{
application.Response.Status = "404 Not Found";
application.Response.StatusCode = 404;
application.Response.StatusDescription = "Not Found";
currentContext.Server.Transfer(errorPage, true);
}
}
}
}
Could someone please explain to me what I am doing wrong, and how IIS 7 Integrated Managed Pipeline Mode works? Because most of the answers I found are about configuring HttpModules
for IIS 6.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,你走在正确的道路上。您是否确保站点的应用程序池设置为托管管道模式?
此外,如果您使用内置的 Visual Studio Web 服务器 (Cassini) 对此进行测试,则
部分将被忽略。如果您希望从那里加载模块,则需要 IIS7 或 IIS7.5 Express。From what I can see you're on the right track. Have you made sure your site's application pool is set to Managed Pipeline mode?
Also if you're testing this with the built in Visual Studio web server (Cassini) then the
<system.webServer>
section will be ignored. You'll need IIS7 or IIS7.5 Express if you want the module to load from there.我遇到了这个问题,发现不关闭 customErrors 会阻止处理程序触发。
即:在您的配置中这是必需的,以便在 HttpModule 中捕获错误事件:
I had this problem and discovered that not turning off customErrors prevented the handler from triggering.
ie: this is required in your config for the Error event to be captured in the HttpModule:
我遇到了处理程序未触发的相同问题,通过对上述代码进行以下更改帮助我解决了此问题。我没有创建新的事件处理程序,而是将具有相同签名的方法附加到该事件。
这对我有用,仍在分析这种在 IIS7 中附加处理程序的方式背后的原因。
I was experiencing the same problem of a handler that is not getting triggered, by doing following change to the above code helped me to resolve this issue. Instead of creating a new event handler I just attached the method with same signature to that event.
This works for me, still analyzing what is the reason behind this way attaching a handler works in IIS7.