Session is null when calling method from one controller to another...MVC

11,170

Solution 1

That's because ControllerB needs to initializes itself, and as part of this process it also sets Session, Request, Resposne etc accordingly.

So, you need to call the Initialize() method and pass it the current RequestContext. But, since it's marked as protected (because it wasn't meant to be called directly, only using the ControllerFactory), you'll have to expose it:

public class ControllerB : Controller
{
    public void InitializeController(RequestContext context)
    {
        base.Initialize(context);
    }
}

Then in your ControllerA:

var controllerB = new ControllerB();
controllerB.InitializeController(this.Request.RequestContext);

Alternatively, since the Session getter is actually a shorthand for this.ControllerContext.HttpContext.Session (same for Request, Response etc), you can set the ControllerContext instead:

var controllerB = new ControllerB();
controllerB.ControllerContext = new ControllerContext(this.Request.RequestContext, controllerB);

See MSDN

Solution 2

This is happening because Session is a property that is initialized different based on how the controller is called.

Instead of Session you can use HttpContext.Session. HttpContext refers to the current http request.

public object CallMethod(){
    if (HttpContext.Session["Grid"] != null)//session object is null
    {
        //do my thing
    }
}

Solution 3

This is easily fixed through the good coding practice of "tell, don't ask", ie you pass the required value into the method, rather than expecting it to go looking for it for itself:

if (Session["Grid"] != null)
{
    var b  = new ControllerB ();
    b.CallMethod(Session["Grid"]);
}

...
public object CallMethod(string grid)
{
    //do your thing
}
Share:
11,170
BoundForGlory
Author by

BoundForGlory

Hello and Greeting from Dallas, I graduated from the University of North Texas with a degree in Electronics Engineering Technology. I'm a software engineer who specializes in ASP.NET MVC, web forms, wpf and winforms. I also do mobile development for Android and iOS. Most importantly, I am loved by God almighty and without Him, I am absolutely nothing. Thanks for reading this and may God bless.

Updated on July 06, 2022

Comments

  • BoundForGlory
    BoundForGlory almost 2 years

    I have an ASP.NET MVC app and i added a new controller to it and call a method from this new controller from an existing controller. I am using session variables and in controller A i call the method in controller B:

    if (Session["Grid"] != null){}//session object is fine here
          ControllerB b  = new ControllerB ();
    b.CallMethod();
    

    In the new controller, which i'm calling B, the method looks like this:

    public object CallMethod(){
        if (Session["Grid"] != null)//session object is null
            {
                //do my thing
            }
      }
    

    The session variable isnt the problem, its the session object. Its completely null, hence my application blows up. The session is alive and well in controller A, so why is it null in controller B? Thank you

  • BoundForGlory
    BoundForGlory almost 9 years
    Hello haim770 - This is what i was looking for. Thank you very much
  • Gokulan P H
    Gokulan P H over 5 years
    You saved my day :)