加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

.net – Application_Error不会触发?

发布时间:2020-12-15 23:58:08 所属栏目:asp.Net 来源:网络整理
导读:在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
在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.

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读