vikram
vikram

Reputation: 69

Session loses it's intermittently in ASP.NET MVC 5

Do you see a reason why the session loses it's value intermittently?

    public ActionResult Index(string app) // this view hits the Create Controller on a form post event
    {
       Session["appType"] = app;         
       return View();
    }
    [HttpPost]
    public ActionResult Create(string userName, string password)
    {
        string a = Session["appType"].ToString(); // throws object reference error.
    }

Upvotes: 1

Views: 244

Answers (1)

Khelvaster
Khelvaster

Reputation: 872

  1. Check the sessionState element in your web.config. You'll lose your session after timeout (in minutes) of inactivity.

You'll see code like:

<sessionState mode="SQLServer"
    cookieless="true "
    regenerateExpiredSessionId="true "
    timeout="30"
    sqlConnectionString="Data Source=MySqlServer;Integrated Security=SSPI;"
    stateNetworkTimeout="30"/>
  1. Could you be running this application behind a load balancer? If so, it might not be routing your users to the same machine for the duration of a session.

Upvotes: 2

Related Questions