Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net mvc is Session available at any point during controller construction?

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?

like image 437
joe rob Avatar asked Jan 22 '23 11:01

joe rob


1 Answers

Override Initialize():

protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
    base.Initialize(requestContext);
    requestContext.HttpContext.Session["blabla"] = "hello"; // do your stuff
}
like image 177
Mauricio Scheffer Avatar answered Jan 24 '23 23:01

Mauricio Scheffer