Application_Error不会触发?

在Webform1.aspx.cs中:
protected void Page_Load(object sender, EventArgs e)
{
    throw new Exception("test exception");
}
在Global.asax.cs中:
protected void Application_Error(object sender, EventArgs e)
{
    // Code that runs when an unhandled error occurs
    if (Server.GetLastError() is HttpUnhandledException)
        Server.Transfer("ErrUnknown.aspx");
}
但是从不调用Application_Error事件处理程序。相反,我得到一个运行时错误页面。 在抛出异常后,我需要做什么才能调用Application_Error?     
已邀请:
它看起来很好,应该调用Application_Error。 你有没有通过
Debugging
检查你的申请? 实际上你错过了
Server.ClearError()
所以异常被传递给asp.net但是你应该在这里压制它,因为你自己处理它。
protected void Application_Error(object sender, EventArgs e)
{
    // Code that runs when an unhandled error occurs
    if (Server.GetLastError() is HttpUnhandledException)
    {
        // suppressing the error so it should not pass to asp.net
        Server.ClearError();
        Server.Transfer("ErrUnknown.aspx");
    }
}
    
我发现了这个问题。
Server.Transfer("ErrUnknown.aspx")
是原因。 在尝试直接在浏览器中查看“ErrUnknown.aspx”时,我意识到我在该页面中出错了。纠正它后,Server.Transfer工作 虽然在调试应用程序时事件不会被触发,但是会产生误导吗? 无论如何。     

要回复问题请先登录注册