c# - 什么会导致 Application_Error 不被调用?

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

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

我有一个在我的机器和其他机器上运行的 ASP.NET 项目,每次我尝试调节 QueryString 时都会收到 System.Exception 抛出的错误

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

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


举个例子,这是我的网页(不能像这样调用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 上运行

它可以是什么?

最佳答案

注释掉代码中的 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();

关于c# - 什么会导致 Application_Error 不被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1498947/

相关文章:

c# - 如何使用超时进行 try-catch?

c# - 使用 MVC Controller 重载

asp.net - 将 ASP.NET MVC 3 项目升级到 ASP.NET MVC 4

java - 在 Java 中如何抛出和捕获异常?

python - 当你抛出异常时,Python 对象会发生什么

c# - 使用查询字符串解析 AppSettings 值时出错

c# - 使用 Kinect 缩放图像不平滑 - 进近时需要更正

c# - 关闭一个子窗体会关闭另一个子窗体

asp.net - 将 SASS 与 ASP.NET 结合使用

asp.net - Facebook Connect 和 ASP.NET