Application_Error 不触发?

发布于 2024-10-31 11:06:33 字数 528 浏览 5 评论 0原文

在 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 该怎么办?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青春如此纠结 2024-11-07 11:06:33

看起来不错,并且应该调用 Application_Error。

您是否通过调试检查了您的应用程序?

实际上,您缺少 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");
    }
}

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");
    }
}
陌伤浅笑 2024-11-07 11:06:33

我发现了问题。

Server.Transfer("ErrUnknown.aspx")

是原因。

当尝试直接在浏览器中查看“ErrUnknown.aspx”时,我意识到该页面中有一个错误。更正后,Server.Transfer 可以正常工作,

但该事件在调试应用程序时不会被触发,这具有误导性?

反正。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文