0
我有一個MVC 4網絡應用程序。我在Global.asax.cs文件的MvcApplication類的「Session_Start」中定義的會話中保存了一個值。無法檢索HttpContext.Current.Session狀態的值
我想從Controller中檢索值。但它拋出了一個錯誤。
protected void Session_Start(Object sender, EventArgs e)
{
int temp = 0;
HttpContext.Current.Session.Add("_SessionCompany", temp);
Debug.WriteLine("session started");
}
在控制器的構造函數:
public LocationController()
{
if (HttpContext.Session == null)
Debug.WriteLine("session is null");
//this.HttpContext.Session["_SessionCompany"] = (int)this.Session["_SessionCompany"] + 1;
//Debug.WriteLine("LocationController instantiated - " + (int)this.Session["_SessionCompany"]);
}
的錯誤是對的HttpContext:
System.NullReferenceException: Object reference not set to an instance of an object.
看來HttpContext的是空對象。
我在哪裏做錯了?
謝謝!
可能重複[ASP.Net MVC控制器構造函數中的會話空](http://stackoverflow.com/questions/889516/session-null-in-asp-net-mvc-controller-constructors) – 2014-11-21 12:01:34