我正在使用ASP.NET MVC,我需要在Application_BeginRequest
上設置會話變量。問題在於此時對象HttpContext.Current.Session
始終是null
。在Application_BeginRequest中設置會話變量
protected void Application_BeginRequest(Object sender, EventArgs e)
{
if (HttpContext.Current.Session != null)
{
//this code is never executed, current session is always null
HttpContext.Current.Session.Add("__MySessionVariable", new object());
}
}
產品/排序重複的:http://stackoverflow.com/questions/765054/whens-the-earliest-i-can -access-some-session-data-in-global-asax/ – 2013-03-01 05:54:52