尝试将 Global.asax 1.0 文件转换为 3.5 出现 Application_Error 问题 +会话和重定向

发布于 2024-08-17 23:32:53 字数 1006 浏览 8 评论 0原文

所以在 Global.asax 中是这样的:

protected void Application_Error(object sender, System.EventArgs { Session["CustomError"] = Server.GetLastError(); 服务器.ClearError(); Response.Redirect("~/ErrorPage.aspx"); 在

ErrorPage.aspx 中是这样的:

private void Page_Load(object sender, System.EventArgs e) { 异常 currentException = ((Exception)Session["CustomError"]).InnerException;

// 写入错误信息 if (当前异常!= null) txtErrorMessage.Text = currentException.Message;

// 循环遍历内部异常。 currentException = (Exception)Session["CustomError"]; while (当前异常!= null) { message.Append(currentException.Message).Append("\r\n").Append(currentException.StackTrace); message.Append("\n============================================ ===\n"); currentException = currentException.InnerException; 由于

这是旧的 1.0 代码,因此在转换为 3.5 Global.asax 文件时会崩溃。它告诉我“会话”不可用并且我无法重定向?我认为问题之一可能是 Application_Start 也抛出了一个错误。但是,如果我注释掉所有应用程序启动代码,我仍然会收到错误,但它们永远不会被重定向到错误页面。

So in the Global.asax is this:

protected void Application_Error(object sender, System.EventArgs
{
Session["CustomError"] = Server.GetLastError();
Server.ClearError();
Response.Redirect("~/ErrorPage.aspx");
}

And in ErrorPage.aspx is this:

private void Page_Load(object sender, System.EventArgs e)
{
Exception currentException = ((Exception)Session["CustomError"]).InnerException;

// Writes the error message
if (currentException != null)
txtErrorMessage.Text = currentException.Message;

// Loops through the inner exceptions.
currentException = (Exception)Session["CustomError"];
while (currentException != null)
{
message.Append(currentException.Message).Append("\r\n").Append(currentException.StackTrace);
message.Append("\n==============================================\n");
currentException = currentException.InnerException;
}

As this is old 1.0 code it barfs when converted to a 3.5 Global.asax file. It tells me that "Session" is not available and also that I can't redirect?? I think one of the issues may be that there is also an error being thrown from Application_Start. But if I comment out all the application start code I still get errors but they never get redirected to the error page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魂归处 2024-08-24 23:32:53

此链接可能会有所帮助:异常陷阱!
此外,使用 web.config 文件定义错误的默认重定向页面。

This link might help: Exceptional Gotchas!.
In addition, use the web.config file to define your default redirect page for errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文