views:

127

answers:

4

I have a controller that stores various info (Ie. FormID, QuestionAnswerList, etc). Currently I am storing them in the Controller.Session and it works fine.

I wanted to break out some logic into a separate class (Ie. RulesController), where I could perform certain checks, etc, but when I try and reference the Session there, it is null. It's clear that the Session remains valid only within the context of the specific controller, but what is everyone doing regarding this?

I would imagine this is pretty common, you want to share certain "global" variables within the different controllers, what is best practice?

Here is a portion of my code:

In my BaseController class:

    public List<QuestionAnswer> QuestionAnswers
    {
        get
        {
            if (Session["QuestionAnswers"] == null)
            {
                List<QuestionAnswer> qAnswers = qaRepository.GetQuestionAnswers(CurrentSection, UserSmartFormID);
                Session["QuestionAnswers"] = qAnswers;
                return qAnswers;
            }
            else
            {
                return (List<QuestionAnswer>)Session["QuestionAnswers"];
            }
        }
        set
        {
            Session["QuestionAnswers"] = value;
        }
    }

In my first Controller (derived from BaseController):

QuestionAnswers = qaRepository.GetQuestionAnswers(CurrentSection, UserSmartFormID);

I stepped through the code and the above statement executes fine, setting the Session["QuestionAnswers"], but then when I try to get from another controller below, the Session["QuestionAnswers"] is null!

My second controller (also derived from BaseController):

List<QuestionAnswer> currentList = (List<QuestionAnswer>)QuestionAnswers;

The above line fails! It looks like the Session object itself is null (not just Session["QuestionAnswers"])

+1  A: 

does it make a difference if you retrieve your session using

HttpContext.Current.Session("mySpecialSession")  ''# note this is VB, not C#
rockinthesixstring
This doesn't exist in the MVC controller, the closest I see is the HttpContext.CurrentHandler, but I believe the Controller Session object is System.Web.HttpSessionBase which should work, shouldn't it?
Mark Kadlec
+1  A: 

I believe TempData will solve your problem, it operates with in the session and persists across multiple requests, however by default it will clear the stored data once you access it again, if that's a problem you can tell it to keep the info with the newly added Keep() function.

So in your case: ... TempData["QuestionAnswers"] = qAnswers; ...

There's much more info at: http://weblogs.asp.net/jacqueseloff/archive/2009/11/17/tempdata-improvements.aspx

matto0
+1  A: 

Where are you accessing the session in the second controller? The session object is not available in the constructor because it is injected later on in the lifecycle.

DancesWithBamboo
The session is accessed from the property of the BaseController (the second controller is also derived from BaseController).
Mark Kadlec
Yes, but do you try to use your QuestionAnswers property in the constructor of the second controller?
DancesWithBamboo
A: 

Ok, finally got it working, although a bit kludgy. I found the solution from another related SO post.

I added the following to my BaseController:

        public new HttpContextBase HttpContext
    {
        get
        {
            HttpContextWrapper context =
                new HttpContextWrapper(System.Web.HttpContext.Current);
            return (HttpContextBase)context;
        }
    }

Then set/retrieved my Session variables using HttpContext.Session and works fine!

Mark Kadlec