Application_Error does not fire?

眉间皱痕 提交于 2019-11-28 04:07:28

问题


In Webform1.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
    throw new Exception("test exception");
}

In the 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");
}

But the Application_Error event handler never gets called. Instead I get a runtime error page.

What do I have to do have Application_Error being called after an exception is thrown?


回答1:


It looks fine and the Application_Error should being called.

Did you checked by Debugging your application?

Actually you are missing Server.ClearError() so the exception is being passed to asp.net but you should suppress it here because you are handling it by yourself.

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");
    }
}



回答2:


I found the problem.

Server.Transfer("ErrUnknown.aspx")

was the cause.

While trying to view 'ErrUnknown.aspx' directly in the browser I realized I had an error in that page. After correcting it Server.Transfer works

Was is misleading though is that the event doesn't get fired while debugging the application?

Anyway.



来源:https://stackoverflow.com/questions/5604144/application-error-does-not-fire

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!