I have a ASP.Net MVC 5 web application and I want to set a variable in the session so that I can access it later. As I am a beginner I don't know how to achieve it. Below is what I have tried
clsUser user = mdlUser.GetUserForSession();
System.Web.HttpContext.Current.Session["MyValue"] = user.SessionID; // user.SessionID is an integer
int x = System.Web.HttpContext.Current.Session["MyValue"] as int; // Access
But I get error as Error as
The as operator must be used with a reference type or nullable type ('int' is a non-nullable value type)
From the documentation (my emphasis):
int
is a value type, not a reference type, therefore theas
operator cannot be used, so you need the 'classic' method for casting the value.