GnomeCubed
GnomeCubed

Reputation: 594

What do you have to do to get sessions working in MVC5?

What do you have to do to get sessions working in MVC5? In MVC4 I can create a new project and add

Session["TEST"] = "worked";

to a controller and it works without any issues. If I do that in an MVC5 project it works fine in Visual Studio 2012, but when I deploy it to IIS I get a "System.NullReferenceException" exception.

Updated Information:

I have MVC5 running in VS 2012 without issue. I installed all the updates to get that working and have everything working except for sessions. I don't think that MVC5 is the issue, I think it's something about the project template that differs from the MVC4 template.

Here is my controller action that's causing the issue:

    public ActionResult Index()
    {
        ViewBag.Message = "Modify this template.";
        Session["TEST"] = "worked";
        return View();
    }

Potential fix:

Adding runAllManagedModulesForAllRequests="true" to my web.config fixes the issue.

<modules runAllManagedModulesForAllRequests="true">

I can also do the suggested answer here:

Asp.net MVC error with configured managed modules

<remove name="Session" />
<add name="Session" type="System.Web.SessionState.SessionStateModule" preCondition="" />

And that fixes my issue as well. Can anyone provide me some further reading on which one is a better solution and why?

Upvotes: 0

Views: 1068

Answers (1)

Pranav Singh
Pranav Singh

Reputation: 20171

The MVC 5 straight forward is not supported in Visual Studio 2012.

You need to install ASP.NET and Web Tools 2013.1 for Visual Studio 2012 for MVC 5 to work in VS 2012, it has added support for ASP.NET MVC 5 and Web API 2 Templates for vs 2012.

Besides that, I don't think "Session["TEST"] = "worked"; doesn't work" has to do anything with MVC 5.

If you are upgrading MVC4 app to MVC5 follow:

http://www.asp.net/mvc/tutorials/mvc-5/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web-api-2

Upvotes: 2

Related Questions