Application_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 该怎么办?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来不错,并且应该调用 Application_Error。
您是否通过
调试
检查了您的应用程序?实际上,您缺少
Server.ClearError()
,因此异常正在传递给 asp.net,但您应该在此处抑制它,因为您正在自己处理它。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.我发现了问题。
是原因。
当尝试直接在浏览器中查看“ErrUnknown.aspx”时,我意识到该页面中有一个错误。更正后,Server.Transfer 可以正常工作,
但该事件在调试应用程序时不会被触发,这具有误导性?
反正。
I found the problem.
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.