i am trying to access Session variables in the constructor of a controller and ControllerContext it seems is always null.
When is the earliest the session variables are available?
thanks!
Edit: Example:
in one controller:
public HomeController()
{
MyClass test = (MyClass)ControllerContext.HttpContext.Session["SessionClass"];
//ControllerContext always null
}
when debugging, controllercontext is ALWAYS null. In the controller whose actionresult redirects to this controller, i have:
Session["SessionClass"] = class;
MyClass test = (MyClass )ControllerContext.HttpContext.Session["SessionClass"];
// this works fine! i can get varibale from session
return RedirectToAction("Index", "Home");
So, at what point is ControllerContext actually set? When can I access session variables?
Override Initialize():
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
base.Initialize(requestContext);
requestContext.HttpContext.Session["blabla"] = "hello"; // do your stuff
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With