如果當前的ASP.NET會話為空,我該怎么辦?在我的Web應用程序中,我執行類似這樣的操作來讀取會話變量:if (HttpContext.Current.Session != null && HttpContext.Current.Session["MyVariable"] != null){
string myVariable= (string)HttpContext.Current.Session["MyVariable"];}我理解為什么重要的是檢查為什么HttpContext.Current.Session [“MyVariable”]為空(該變量可能尚未存儲在會話中,或者會因各種原因重置了會話),但為什么我需要檢查如果HttpContext.Current.Session是null?我的理解是,會話是由ASP.NET自動創建的,因此HttpContext.Current.Session永遠不應為null。這個假設是否正確?如果它可以為null,是否意味著我還應該在存儲內容之前檢查它:if (HttpContext.Current.Session != null){
HttpContext.Current.Session["MyVariable"]="Test";}else{
// What should be done in this case (if session is null)?
// Is it possible to force the session to be created if it doesn't exist?}
如果當前的ASP.NET會話為空,我該怎么辦?
慕的地6264312
2019-07-26 17:04:17