【问题标题】:What can cause Application_Error not to be invoked?什么会导致 Application_Error 不被调用?
【发布时间】:2010-12-02 16:57:18
【问题描述】:

在过去的 2 周里,我遇到了一个我无法弄清楚的案例,也许你们已经解决了同样的问题或者听到/阅读过它并且可以帮助我。

我有一个 ASP.NET 项目,我在我的机器和其他机器上运行良好,每次我尝试调整 QueryString 时都会收到由 System.Exception 引发的错误

问题是,在这台特定的机器上(女巫在荷兰),Application_Error 永远不会捕获异常,它会将异常消息保存到日志中(就像我的应用程序中应该做的那样),但它不会“破坏”Web 应用程序...它只是继续!

如何以及是什么使这成为可能?


举个例子,这是我的网页(不能像这样调用HttpCache,但只是指出我正在使用Web.HttpCache对象)

if( Request.QueryString["user"] != HttpCache["MYAPP-user"] ) {
   myApp.DebugWrite("User was tempered.");
   throw System.Exception("User was tempered.");
}

在 global.asax 我有

...

void Application_Error(object sender, EventArgs e)
{
    // Code that runs when an unhandled error occurs
    if (Server.GetLastError() != null)
    {
        Exception objErr = Server.GetLastError().GetBaseException();
        String url = Request.Url.ToString();
        String msg = objErr.Message;
        String trc = objErr.StackTrace.ToString();


        Response.Clear();

        StringBuilder html = new StringBuilder();
        // fill up html with html code in order to present a nice message
        Response.Write(html.ToString()); 

        Server.ClearError();
        Response.Flush();
        Response.End();
    }
}

...

在我的测试机器中,我总是得到带有错误消息的 html,以及日志中的消息......在这台特定的机器上,我只在日志中看到错误,但 应用程序继续!!!

web.config 与所有机器中的完全相同,并且此 Web 应用程序在 .NET 2.0 上运行

会是什么?

【问题讨论】:

    标签: c# asp.net exception-handling error-handling global-asax


    【解决方案1】:

    注释掉代码中的 Response 和 Server.ClearError() 部分:

     //Response.Clear();
    
                    StringBuilder html = new StringBuilder();
                    // fill up html with html code in order to present a nice message
                    Response.Write(html.ToString());
    
                    //Server.ClearError();
                    //Response.Flush();
                    //Response.End();
    

    【讨论】:

    • -1 不知道你是否注意到,但异常永远不会到达 Application_Error
    • 您应该刚刚发表评论而不是 -1。 :(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-28
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多