簡體   English   中英

ASP.NET會話狀態服務器:存儲的對象為NULL

[英]ASP.NET Session State Server: Stored Object is NULL

我已經在SQL數據庫上設置了ASP.NET會話狀態服務器:

<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類都標記為Serializable。

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

提前致謝。

好吧,讓它正常工作。

使用會話狀態服務器時,必須在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();  
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM