[英]Getting NullReferenceException and object reference not set to an instance of an object when checking if session is null with ASP.NET 5 and MVC 6
[英]IIS8 MVC asp.NET Session not working. Getting null object reference error
無法使會話變量工作,我已經嘗試了我可以在網上找到的所有解決方案。
在第1頁上,我在c#模型中調用以下內容
HttpContext.Current.Session["lol1"] = "123";
稍后,我使用ajax調用在c#模型中將此會話變量返回給我,
return HttpContext.Current.Session["lol1"].ToString();
在javascript中,我在ajax成功函數中將其選中,並將其放入帶有alert(e);的警報框中;
我收到空對象引用錯誤。 好像我的變量沒有保存到會話中。 這一切都可以通過調試在localhost上正常工作,包括警報。
我嘗試過的事情(當前設置):
-DefaultAppPool設置最大工作進程數:1
-IIS管理器-> ASP->服務->會話屬性->啟用會話狀態:true
-IIS manager-> Session State->正在進行中
-在我的解決方案web.config中:
<system.web>
<sessionState mode="InProc" timeout="25"></sessionState>
</system.web>
<system.webServer>
<modules>
<remove name="Session" />
<add name="Session" type="System.Web.SessionState.SessionStateModule"/>
</modules>
</system.webServer>
嘗試在控制器中設置SessionStateBehavior
。 例如:
[SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]
如果只需要從Session
讀取而不是對其進行寫操作以防止阻塞,則建議使用ReadOnly
。
鏈接到Microsoft的SessionStateBehavior枚舉 。
(有關控制器使用可寫會話時ASP.NET阻塞的更多信息,請參閱此鏈接: 當您使用DynamoDB訪問時,會話狀態是否讀取/寫入所有內容 )
希望這可以幫助!
問候,
羅斯
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.