gpt4 book ai didi

.net - Application_Error 不会触发?

转载 作者:行者123 更新时间:2023-12-04 15:03:52 24 4
gpt4 key购买 nike

在 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 。

您是否通过 Debugging 检查过你的申请?

原来你不见了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");
}
}

关于.net - Application_Error 不会触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5604144/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com