Session changes done in application_Error rolled back after redirect

前端 未结 5 1552
感情败类
感情败类 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: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());
    
    }
    

提交回复
热议问题