0

I have created a model class object in session_Start method in global.asax in MVC 5 application.

vmUserVariables usr = new vmUserVariables();
//Assign values to usr class object from SQL DB
this.Session["_userVariables"] = usr; 

I am getting the session variable value and passing it to a view as model in controller.

public ActionResult EntryFunctions()
{
   var userVariables = this.Session["_userVariables"];
   return View(userVariables);
}

should I check whether the session variable value is null? If so when does my session variable becomes null?

Could I restart the application if the session variable is null, so that the _uservariables is set again in the global.asax like below?

if(this.Session["_userVariables"] == null)
{
   //restart the application so that session_Start in global.asax is called
}
Nagaraj Raveendran
  • 1,150
  • 15
  • 23
  • What actually you want to achieve? Store "vmUserVariables" model in session which can be used during the user session? – DSR Aug 12 '16 at 11:04
  • Yes. Set the vmUserVariables in session_Start in global.asax and use it in the views – Nagaraj Raveendran Aug 12 '16 at 21:09
  • Using session variable's not best practice which could create some issues in your app. You could add important user information in to authentication cookie by extending IPrinciple http://stackoverflow.com/questions/1064271/asp-net-mvc-set-custom-iidentity-or-iprincipal. If you really want to use session variables, then you could create action filter to handle session part which will be creation and check point for your user _userVariables. Then you don't need to check _userVariables inside your methods. – DSR Aug 15 '16 at 12:43

0 Answers0