Stelloy
Stelloy

Reputation: 2336

Session not persisting between requests

Creating this as I looked at MVC4 Session not persisted between requests and various others but none of them helped to solve my issue.

I have the following two action methods on my MVC controller:

    public ActionResult Capture(string testValue)
    {
        Session["Test"] = testValue;
        ViewBag.Test = Session["Test"]; // This works and correctly sets the value
        return View();
    }

    [HttpPost]
    public ActionResult Capture(SomeViewModel viewModel)
    {
        ViewBag.Test = Session["Test"]; // At this point the value in the session is inexplicably null, and thus so is the item in the ViewBag

        return View(viewModel);
    }

When I try and use ViewBag.Test in my view

The same behaviour if I set a breakpoint in the code, the session object is set and has the right value in the initial "Get" but is null in the Post after.

What is happening to my session, why is the value not being persisted between requests?

I have established that the session is being created on every request but cannot see why.

Upvotes: 0

Views: 1750

Answers (1)

Stelloy
Stelloy

Reputation: 2336

Due to this being intended for a secure application, I have the following setting in my web.config: <httpCookies httpOnlyCookies="true" requireSSL="true" />

However, my local environment is not running over HTTPS. This is causing the .net session to be recreated on every single request, as the cookie cannot be issued over a non-secure connection. Removing this line from the core web.config and adding it back for web.release.config transform solved the issue.

Upvotes: 4

Related Questions