Mark Kadlec
Mark Kadlec

Reputation: 8450

How does the session state work in MVC 2.0?

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"])

Upvotes: 2

Views: 3119

Answers (4)

Mark Kadlec
Mark Kadlec

Reputation: 8450

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!

Upvotes: 0

DancesWithBamboo
DancesWithBamboo

Reputation: 4156

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.

Upvotes: 1

matto0
matto0

Reputation: 1095

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

Upvotes: 1

Chase Florell
Chase Florell

Reputation: 47397

does it make a difference if you retrieve your session using

HttpContext.Current.Session("mySpecialSession")  ''# note this is VB, not C#

Upvotes: 1

Related Questions