2015-12-03 90 views
0

我已經建立了一個ASP.NET會話狀態服務器上的SQL數據庫:ASP.NET會話狀態服務器:存儲的對象爲NULL

<sessionState timeout="60" allowCustomSqlDatabase="true" mode="SQLServer" sqlConnectionString="..." /> 

我有一個默認的錯誤重定向:

<customErrors mode="On" defaultRedirect="~/Error.aspx" /> 

的Global.asax.cs:

private void Application_Error(object sender, EventArgs e) 
{ 
    SomeSessionObj sessionObj = new SomeSessionObj(); 
    sessionObj.SomeProperty1 = true; 
    sessionObj.SomeProperty2 = new Blabla(); 
    HttpContext.Current.Session["FooBar"] = sessionObj; 
} 

Error.aspx.cs:

protected void Page_Load(object sender, EventArgs e) 
{ 
    SomeSessionObj sessionObj = HttpContext.Current.Session["FooBar"] as SomeSessionObj; 
    // sessionObj is NOT NULL 
    // sessionObj.SomeProperty1 is false 
    // sessionObj.SomeProperty2 is NULL 
} 

SomeSessionObj和SomeProperty類都被標記爲可序列化。

沒有狀態服務器(inProc)它按預期工作。

在此先感謝。

回答

0

好的,得到它的工作。

當使用會話狀態服務器時,您必須在Application_Error中調用Server.ClearError(),否則請求將被終止並且會話狀態將不會被寫入。

void Application_Error(object sender, EventArgs e) 
{ 
    Exception ex = Server.GetLastError(); 
    HttpContext.Current.Session["Error"] = ex.Message; 
    Response.Redirect("error.aspx",false); 
    Server.ClearError(); 
}