I developed an ASP.NET MVC 4 web application (.net 4.5) that works fine in Visual Studio 2012. After deploying to IIS 7 on Windows Server 2008 R2, it seems like the HttpContext.Session object inside my controller is zero, I created a simple ASP.NET MVC 4 application to demonstrate the problem.
In my test application, I have a simple Home Controller
public class HomeController : Controller { public ActionResult Index() { if ( HttpContext != null && HttpContext.Session != null ) { HttpContext.Session[ "test" ] = "a string in session state"; ViewBag.Info = HttpContext.Session[ "test" ]; return View(); } else { if ( HttpContext == null ) { ViewBag.Info = "beeeeuuuuu - HttpContext = null!!!"; } else if ( HttpContext.Session == null ) { ViewBag.Info = "beeeeuuuuu - Session = null!!!"; } return View(); } } }
My Index.chtml looks something like this:
@{ ViewBag.Title = "Index"; } <h2>Index</h2> This is a simple test <p>@ViewBag.Info</p>
Therefore, when I run the application, I get what I expect:
Index This is a simple test a string in session state
But after deploying the application to the web server, the website gives me the following page indicating that the Session object is null:
Index This is a simple test beeeeuuuuu - Session = null!!!
The web application is deployed to the default website, which runs under the ASP.NET v4.0 application pool (integrated pipeline).
I already reinstalled ASP.NET on the server using aspnet_regiis -ir, but that did not help. Session state is enabled (In Proc) on the ISS server. I hope someone can help me here because I have been trying to solve this for quite a while.
Thanks a lot in advance and are kind.
UPDATE: I also tested building ASP.NET MVC 4 with .NET 4.0 instead of 4.5 and had the same problem. I also deployed an ASP.NET Web Pages application (.NET 4.0) and it works fine (HttpContext.Current.Session does not have null code in the code).
UPDATE II: I also tried to save the session state in a database that worked fine on my development machine but had the same problem on the production server (HttpContext.Session still returns null).