Iis 7 IIS7集成管道模式中的异常处理

Iis 7 IIS7集成管道模式中的异常处理,iis-7,exception-handling,integrated-pipeline-mode,Iis 7,Exception Handling,Integrated Pipeline Mode,我有一个应用程序托管在IIS7上,以集成模式运行。我通过将以下内容放入Web.config来处理错误: <httpErrors errorMode="DetailedLocalOnly" existingResponse="Replace" defaultResponseMode="ExecuteURL" defaultPath="/Error.aspx"> <remove statusCode="500" /> <error st

我有一个应用程序托管在IIS7上,以集成模式运行。我通过将以下内容放入Web.config来处理错误:

<httpErrors errorMode="DetailedLocalOnly" existingResponse="Replace" 
            defaultResponseMode="ExecuteURL" defaultPath="/Error.aspx">
  <remove statusCode="500" />
  <error statusCode="500" path="/Error.aspx" responseMode="ExecuteURL" />
</httpErrors>
但它什么也不返回。我还尝试了HttpContext.Current.Error和HttpContext.Current.allerror,但它们都不起作用


在IIS7集成模式下运行的自定义错误页面中,如何获取对已处理异常的引用?

您需要在Global.asax或自定义IHttpModule实现中拦截错误,如下所示:

public class UnhandledExceptionHandlerModule : IHttpModule {
    private HttpApplication application;

    public void Init(HttpApplication application)
    {
        this.application = httpApplication;
        this.application.Error += Application_Error;
    }

    public void Dispose()
    {
        application = null;
    }

    protected internal void Application_Error(object sender, EventArgs e)
    {
        application.Transfer("~/Error.aspx");
    }
}
然后,在Error.aspx.cs中:

protected void Page_Load(object sender, EventArgs e) {
    Response.StatusCode = 500;

    // Prevent IIS from discarding our response if
    // <system.webServer>/<httpErrors> is configured.
    Response.TrySkipIisCustomErrors = true;

    // Send error in email
    SendEmail(Server.GetLastError());

    // Prevent ASP.NET from redirecting if
    // <system.web>/<customErrors> is configured.
    Server.ClearError();
}
protected void Page_Load(object sender, EventArgs e) {
    Response.StatusCode = 500;

    // Prevent IIS from discarding our response if
    // <system.webServer>/<httpErrors> is configured.
    Response.TrySkipIisCustomErrors = true;

    // Send error in email
    SendEmail(Server.GetLastError());

    // Prevent ASP.NET from redirecting if
    // <system.web>/<customErrors> is configured.
    Server.ClearError();
}