C# 在应用程序中完成的会话更改\u重定向后回滚错误
我有一个asp.net应用程序,我正在尝试使用应用程序错误处理自定义异常。它工作得很好,但我有一个新的要求,即在会话对象(或任何可以维护状态的对象)中设置错误数据,并重定向到error.aspx页面。在这里,我只是在读取错误消息后删除它们 所以,无论我在Application_error和调用Redirect时向session对象添加了什么(或者如果我更改了以前存在的值)。在error.aspx中,我看不到任何值,就好像在Application\u error中会话处于只读模式一样 我打过电话 session.IsReadOnlyC# 在应用程序中完成的会话更改\u重定向后回滚错误,c#,asp.net,C#,Asp.net,我有一个asp.net应用程序,我正在尝试使用应用程序错误处理自定义异常。它工作得很好,但我有一个新的要求,即在会话对象(或任何可以维护状态的对象)中设置错误数据,并重定向到error.aspx页面。在这里,我只是在读取错误消息后删除它们 所以,无论我在Application_error和调用Redirect时向session对象添加了什么(或者如果我更改了以前存在的值)。在error.aspx中,我看不到任何值,就好像在Application\u error中会话处于只读模式一样 我打过电话
查看其是否为只读。但它返回错误 检查此项,问题在于重定向调用
检查此项,问题在于重定向调用
您可以对密钥使用缓存和适当的命名策略,如下所示:
protected void Application_Error(object sender, EventArgs e)
{
HttpContext.Current.Cache["error:" + Session.SessionID] = Server.GetLastError();
Response.Redirect("Error.aspx");
}
protected void Application_Error(object sender, EventArgs e)
{
// Grab the last exception
Exception ex = Server.GetLastError();
// put it in session
Session["Last_Exception"] = ex;
// clear the last error to stop .net clearing session
Server.ClearError();
// The above stops the auto-redirect - so do a redirect!
Response.Redirect("~/error.aspx");
}
protected void Application_Error(object sender, EventArgs e)
{
// Grab the last exception
Exception ex = Server.GetLastError();
// put it in session
Session["Last_Exception"] = ex;
// clear the last error to stop .net clearing session
Server.ClearError();
// The above stops the auto-redirect - so do a redirect using the default redirect from the customErrors section of the web.config!
var customerrors = (CustomErrorsSection)WebConfigurationManager.OpenWebConfiguration("/").GetSection("system.web/customErrors");
Response.Redirect(customerrors.DefaultRedirect);
}
在Error.aspx页面中,您可以这样阅读:
var error = Cache["error:" + Session.SessionID];
您可以对密钥使用缓存和适当的命名策略,如下所示:
protected void Application_Error(object sender, EventArgs e)
{
HttpContext.Current.Cache["error:" + Session.SessionID] = Server.GetLastError();
Response.Redirect("Error.aspx");
}
protected void Application_Error(object sender, EventArgs e)
{
// Grab the last exception
Exception ex = Server.GetLastError();
// put it in session
Session["Last_Exception"] = ex;
// clear the last error to stop .net clearing session
Server.ClearError();
// The above stops the auto-redirect - so do a redirect!
Response.Redirect("~/error.aspx");
}
protected void Application_Error(object sender, EventArgs e)
{
// Grab the last exception
Exception ex = Server.GetLastError();
// put it in session
Session["Last_Exception"] = ex;
// clear the last error to stop .net clearing session
Server.ClearError();
// The above stops the auto-redirect - so do a redirect using the default redirect from the customErrors section of the web.config!
var customerrors = (CustomErrorsSection)WebConfigurationManager.OpenWebConfiguration("/").GetSection("system.web/customErrors");
Response.Redirect(customerrors.DefaultRedirect);
}
在Error.aspx页面中,您可以这样阅读:
var error = Cache["error:" + Session.SessionID];
将指令添加到Global.asax文件,导入System.Web命名空间
<%@ Import Namespace="System.Web" %>
void Application_Error(object sender, EventArgs e) {
string message = Server.GetLastError().Message;
Session["error"] = message;
Server.Transfer("Error.aspx");
}
将指令添加到Global.asax文件,导入System.Web命名空间
<%@ Import Namespace="System.Web" %>
void Application_Error(object sender, EventArgs e) {
string message = Server.GetLastError().Message;
Session["error"] = message;
Server.Transfer("Error.aspx");
}
问题可能是在应用程序中引发异常时,事件可能未激发,只有在触发此特定事件后,才能设置会话值,因为这是设置应用程序状态的阶段问题可能是,在应用程序中引发异常时,事件可能没有触发,只有在触发此特定事件后,您才能设置会话值,因为这是设置应用程序状态的阶段我自己刚刚遇到了这个问题,经过几个小时的胡闹,终于解决了这个问题。问题是,
Application\u Error()
在作为某种需要停止的“清理”例程的一部分执行后清除会话
我找到的解决方案如下:
Server.ClearError()代码>-这将清除应用程序中的最后一个错误,并停止“清除”操作,从而保留会话
Response.redirect(“~/error.aspx”)代码>
protected void Application_Error(object sender, EventArgs e)
{
HttpContext.Current.Cache["error:" + Session.SessionID] = Server.GetLastError();
Response.Redirect("Error.aspx");
}
protected void Application_Error(object sender, EventArgs e)
{
// Grab the last exception
Exception ex = Server.GetLastError();
// put it in session
Session["Last_Exception"] = ex;
// clear the last error to stop .net clearing session
Server.ClearError();
// The above stops the auto-redirect - so do a redirect!
Response.Redirect("~/error.aspx");
}
protected void Application_Error(object sender, EventArgs e)
{
// Grab the last exception
Exception ex = Server.GetLastError();
// put it in session
Session["Last_Exception"] = ex;
// clear the last error to stop .net clearing session
Server.ClearError();
// The above stops the auto-redirect - so do a redirect using the default redirect from the customErrors section of the web.config!
var customerrors = (CustomErrorsSection)WebConfigurationManager.OpenWebConfiguration("/").GetSection("system.web/customErrors");
Response.Redirect(customerrors.DefaultRedirect);
}
如果您不想硬编码URL,可以直接从web.config
中的customerrors
部分获取defaultRedirect
URL,这将为您提供如下内容:
protected void Application_Error(object sender, EventArgs e)
{
HttpContext.Current.Cache["error:" + Session.SessionID] = Server.GetLastError();
Response.Redirect("Error.aspx");
}
protected void Application_Error(object sender, EventArgs e)
{
// Grab the last exception
Exception ex = Server.GetLastError();
// put it in session
Session["Last_Exception"] = ex;
// clear the last error to stop .net clearing session
Server.ClearError();
// The above stops the auto-redirect - so do a redirect!
Response.Redirect("~/error.aspx");
}
protected void Application_Error(object sender, EventArgs e)
{
// Grab the last exception
Exception ex = Server.GetLastError();
// put it in session
Session["Last_Exception"] = ex;
// clear the last error to stop .net clearing session
Server.ClearError();
// The above stops the auto-redirect - so do a redirect using the default redirect from the customErrors section of the web.config!
var customerrors = (CustomErrorsSection)WebConfigurationManager.OpenWebConfiguration("/").GetSection("system.web/customErrors");
Response.Redirect(customerrors.DefaultRedirect);
}
我自己刚遇到这个问题,经过几个小时的胡闹,我终于解决了。问题是,
Application\u Error()
在作为某种需要停止的“清理”例程的一部分执行后清除会话
我找到的解决方案如下:
Server.ClearError()代码>-这将清除应用程序中的最后一个错误,并停止“清除”操作,从而保留会话
Response.redirect(“~/error.aspx”)代码>
protected void Application_Error(object sender, EventArgs e)
{
HttpContext.Current.Cache["error:" + Session.SessionID] = Server.GetLastError();
Response.Redirect("Error.aspx");
}
protected void Application_Error(object sender, EventArgs e)
{
// Grab the last exception
Exception ex = Server.GetLastError();
// put it in session
Session["Last_Exception"] = ex;
// clear the last error to stop .net clearing session
Server.ClearError();
// The above stops the auto-redirect - so do a redirect!
Response.Redirect("~/error.aspx");
}
protected void Application_Error(object sender, EventArgs e)
{
// Grab the last exception
Exception ex = Server.GetLastError();
// put it in session
Session["Last_Exception"] = ex;
// clear the last error to stop .net clearing session
Server.ClearError();
// The above stops the auto-redirect - so do a redirect using the default redirect from the customErrors section of the web.config!
var customerrors = (CustomErrorsSection)WebConfigurationManager.OpenWebConfiguration("/").GetSection("system.web/customErrors");
Response.Redirect(customerrors.DefaultRedirect);
}
如果您不想硬编码URL,可以直接从web.config
中的customerrors
部分获取defaultRedirect
URL,这将为您提供如下内容:
protected void Application_Error(object sender, EventArgs e)
{
HttpContext.Current.Cache["error:" + Session.SessionID] = Server.GetLastError();
Response.Redirect("Error.aspx");
}
protected void Application_Error(object sender, EventArgs e)
{
// Grab the last exception
Exception ex = Server.GetLastError();
// put it in session
Session["Last_Exception"] = ex;
// clear the last error to stop .net clearing session
Server.ClearError();
// The above stops the auto-redirect - so do a redirect!
Response.Redirect("~/error.aspx");
}
protected void Application_Error(object sender, EventArgs e)
{
// Grab the last exception
Exception ex = Server.GetLastError();
// put it in session
Session["Last_Exception"] = ex;
// clear the last error to stop .net clearing session
Server.ClearError();
// The above stops the auto-redirect - so do a redirect using the default redirect from the customErrors section of the web.config!
var customerrors = (CustomErrorsSection)WebConfigurationManager.OpenWebConfiguration("/").GetSection("system.web/customErrors");
Response.Redirect(customerrors.DefaultRedirect);
}
我看过这个。我实际上是在用false来称呼它,没有回应。end()。。让我把代码贴在这里,我已经看过了。我实际上是在用false来称呼它,没有回应。end()。。让我把代码贴在这里。你解决过这个问题吗?别担心,我已经解决了,并在下面添加了答案。你解决过这个问题吗?别担心,我已经解决了,并在下面添加了答案。这是一个完美的解决方案!工作起来很有魅力,解释得也很好。谢谢。这是一个完美的解决方案!工作起来很有魅力,解释得也很好。谢谢