MVC4 Session not persisted between requests

Danny van der Kraan

BIG update after some good testing

I have a MVC4 web application and I'm simply debugging it in visual studio 2010 just to learn some more about webdevelopment (and MVC in particular). I'm playing with Session now. But I don't understand why I lose my variables after a new httprequest.

It's like this question: Losing my session variables

My web.config for the session part looks like:

<sessionState mode="InProc"
   cookieless="false"
   timeout="20"/>

My little test project to isolate the problem works fine and looks like:

Controllers - HomeController

public class HomeController : Controller
{
    //
    // GET: /Test/

    public ActionResult Index()
    {
        string t = (string)Session["Test1"];
        ViewBag.Result = t;
        return View();
    }

}
  • MysessionController

    public class MysessionController : Controller { // // GET: /Mysession/

        public ActionResult Index()
        {
            return View(new Models.Mysession() {ID = Session.SessionID});
        }
    
        [HttpPost]
        public ActionResult Index(Models.Mysession mySession) {
            Session["Test1"] = "Bla";
            return RedirectToAction("Index", "Home");
        }
    
    }
    

Models

public class Mysession {
    [Required]
    public string ID { get; set; }
}

Views

  • Home

    • Index.cshtml

      @{ ViewBag.Title = "Index"; }

      Index

      @@ViewBag.Result@ @((string)Session["Test1"]) @Html.ActionLink("My session", "Index", "Mysession")

  • Mysession

    • Index.cshtml

      @model SessionTest.Models.Mysession @{ ViewBag.Title = "Index"; }

    Index

    @using (Html.BeginForm()) {

    @Html.ValidationSummary(false)
    <fieldset>
        <legend>Mysession</legend>
         <div class="editor-label">
            @Html.LabelFor(model => model.ID)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ID)
        </div>
        <p>
            <input type="submit" value="Log in" />
        </p>
    </fieldset>
    

    }

This looks in a nutshell like my main application, the DIFFERENCE is: In MysessionController in the HTTPPOST Index action where the session variable is set I acces a XML file to look up something.

The XML file is my persistent storage because I don't have a SQL server. My question is, can this affect my session?

If so, I would like to point out that I acces the XML file PRIOR to setting the variable in the session. So it seems strange to me. But I've pinpointed by testing that if I don't acces the XML file then the vars in the session are fine. If I acces the XML file the vars in the session are null after the RedirectToAction.

I don't know why I didn't came to this conclusion yesterday. Sorry everybody. I did some sloppy testing yesterday.

Danny van der Kraan

First off, I would like to thank everybody that has put time in this question. After I got myself together and finally conducted a proper test I pinpointed the problem to accessing a XML file on the hard drive. Don't know what I was messing about yesterday...

I found trough Google several posts and articles about losing your session after accessing files. Like this one:

http://blogs.msdn.com/b/tess/archive/2006/08/02/asp-net-case-study-lost-session-variables-and-appdomain-recycles.aspx

Fantastic case study by Tessa Ferrandez. And to lesser extend: http://forums.asp.net/t/998370.aspx/1

And:

http://blogs.msdn.com/b/toddca/archive/2005/12/01/499144.aspx

Conclusion

When you access files in any way of your website the AppDomain will be refreshed and this will refresh your session.

Never put a file you need to access through your website in your 'bin' folder. I've moved my XML file to App_Data and the variables are persisted in the Session.

Summed up my answer on my blog: http://dannyvanderkraan.wordpress.com/2013/08/21/losing-session-variables-with-asp-net/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Rails 4 session is not updated between requests

From Dev

Best way to share session between MVC4 and a class library

From Dev

python-requests keep session between function

From Dev

Flask session variable not persisting between requests

From Dev

Difference between using requests.get() and requests.session().get()?

From Dev

Is it possible to "transfer" a session between selenium.webdriver and requests.session

From Dev

ASP MVC4 + NHibernate objects in Session

From Dev

MVC4 and ServiceStack session in Redis

From Dev

MVC4 and ServiceStack session in Redis

From Dev

Safari localStorage not persisted between sessions

From Dev

Data not persisted between steps in wizard

From Dev

MVC4 - Relationship between a View and a Controller

From Dev

How is the express req.session object persisted?

From Dev

Pass Injected EntityManager to a web session persisted object

From Dev

Save a session/state data between multiple requests in Service Stack framework

From Dev

Rails session store to persist search params between requests

From Dev

Strange behaviour of cherrypy.session while testing between requests

From Dev

MVC4 RDLC Stops all other requests

From Dev

Redirect to specific page after session expires (MVC4)

From Dev

Reconnecting to Servicestack session in an asp.net MVC4 application

From Dev

Is it possible to get session values from action to another in MVC4?

From Dev

Session without authentication with MemoryCacheClient in servicestack with MVC4

From Dev

Not redirecting to login page after session expires in mvc4

From Dev

Setting session timeout in asp.net mvc4?

From Dev

Unable to return to login page after session expires in mvc4

From Dev

Are NSUndomanager changes persisted between launches with CoreData

From Dev

MongoDB prototypal inheritance between persisted objects

From Dev

Is a Python requests Session object shared between gevent greenlets, thread-safe (between greenlets)?

From Dev

Best practice for cache requests on persisted objects in Play/Scala

Related Related

  1. 1

    Rails 4 session is not updated between requests

  2. 2

    Best way to share session between MVC4 and a class library

  3. 3

    python-requests keep session between function

  4. 4

    Flask session variable not persisting between requests

  5. 5

    Difference between using requests.get() and requests.session().get()?

  6. 6

    Is it possible to "transfer" a session between selenium.webdriver and requests.session

  7. 7

    ASP MVC4 + NHibernate objects in Session

  8. 8

    MVC4 and ServiceStack session in Redis

  9. 9

    MVC4 and ServiceStack session in Redis

  10. 10

    Safari localStorage not persisted between sessions

  11. 11

    Data not persisted between steps in wizard

  12. 12

    MVC4 - Relationship between a View and a Controller

  13. 13

    How is the express req.session object persisted?

  14. 14

    Pass Injected EntityManager to a web session persisted object

  15. 15

    Save a session/state data between multiple requests in Service Stack framework

  16. 16

    Rails session store to persist search params between requests

  17. 17

    Strange behaviour of cherrypy.session while testing between requests

  18. 18

    MVC4 RDLC Stops all other requests

  19. 19

    Redirect to specific page after session expires (MVC4)

  20. 20

    Reconnecting to Servicestack session in an asp.net MVC4 application

  21. 21

    Is it possible to get session values from action to another in MVC4?

  22. 22

    Session without authentication with MemoryCacheClient in servicestack with MVC4

  23. 23

    Not redirecting to login page after session expires in mvc4

  24. 24

    Setting session timeout in asp.net mvc4?

  25. 25

    Unable to return to login page after session expires in mvc4

  26. 26

    Are NSUndomanager changes persisted between launches with CoreData

  27. 27

    MongoDB prototypal inheritance between persisted objects

  28. 28

    Is a Python requests Session object shared between gevent greenlets, thread-safe (between greenlets)?

  29. 29

    Best practice for cache requests on persisted objects in Play/Scala

HotTag

Archive