尝试将 Global.asax 1.0 文件转换为 3.5 出现 Application_Error 问题 +会话和重定向
所以在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此链接可能会有所帮助:异常陷阱!。
此外,使用 web.config 文件定义错误的默认重定向页面。
This link might help: Exceptional Gotchas!.
In addition, use the web.config file to define your default redirect page for errors.