Session changes done in application_Error rolled back after redirect

前端 未结 5 1538
感情败类
感情败类 2021-01-26 05:01

I have an asp.net application and I am trying to handle custom exception using Application_Error. It works nicely but I have new requirement to set the error data in Session obj

相关标签:
5条回答
  • 2021-01-26 05:40

    The problem might that the AcquireRequestState event might not have fired when the exception was raised in your application, you will be able to set a session value only after this particular event is fired as this is the stage where the application state is set

    0 讨论(0)
  • 2021-01-26 05:48

    I've just come across this problem myself and, after a couple of hours of messing around, managed to solve it. The issue is that Application_Error() clears the session after executing as part of some sort of "cleanup" routine which you need to stop.

    The solution I found is as follows:

    1. Call Server.ClearError(); - this clears the last error from the application and stops the "cleanup" taking place, thus preserving session.

    2. The (unwanted imho) side-effect of this is that it no longer performs the automatic redirect to the error page, so you need to explicitly call Response.Redirect("~/error.aspx");

    So, something like this:

    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");
    }
    

    If you don't want to hard-code the URL, you could grab the defaultRedirect URL directly from the customerrors section in the web.config, which would give you something like this:

    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);
    }
    
    0 讨论(0)
  • 2021-01-26 05:49

    You could use the Cache and an appropriate naming strategy for your key, like this:

    protected void Application_Error(object sender, EventArgs e)
        {
            HttpContext.Current.Cache["error:" + Session.SessionID] = Server.GetLastError();
            Response.Redirect("Error.aspx");
        }
    

    in the Error.aspx page you ca read it like this:

    var error = Cache["error:" + Session.SessionID];
    
    0 讨论(0)
  • 2021-01-26 05:54

    Check this, the problem is with the redirect call.

    Don't redirect after setting a Session variable (or do it right)

    0 讨论(0)
  • 2021-01-26 05:58

    Add a directive to the Global.asax file, Imports the System.Web Namespace

     <%@ Import Namespace="System.Web" %>
    
    
    void Application_Error(object sender, EventArgs e) {
    
       string message = Server.GetLastError().Message;
       Session["error"] = message;
       Server.Transfer("Error.aspx");
    
    }
    

    Write the error message added in the sesion object in the Error.aspx page

     protected void Page_Load(object sender, EventArgs e){
        if (!this.IsPostBack)
            Response.Write(Session["error"].ToString());
    
    }
    
    0 讨论(0)
提交回复
热议问题