如何在MVC 2.0会话状态的工作? [英] How does the session state work in MVC 2.0?

查看:121
本文介绍了如何在MVC 2.0会话状态的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有保存各种信息(即FormID,QuestionAnswerList等)的控制器。目前,我将它们存储在Controller.Session,它工作正常。

我想打破一些逻辑放到一个单独的类(即RulesController),在那里我可以执行某些检查,等等,但是当我尝试并引用会话那里,它为空。很显然,在会议仅在特定的控制器的情况下仍然有效,但是每个人都在做这方面?

我会想象这是pretty常见的,要在不同的控制器中共享某种全局变量,什么是最好的做法是什么?

下面是我的code的一部分:

在我BaseController类:

 公开名单<&问题答案GT; QuestionAnswers
    {
        得到
        {
            如果(会话[QuestionAnswers​​] == NULL)
            {
                清单<&问题答案GT; qAnswers = qaRepository.GetQuestionAnswers(CurrentSection,UserSmartFormID);
                会话[QuestionAnswers​​] = qAnswers;
                返回qAnswers;
            }
            其他
            {
                返回(列表<&问题答案GT;)会议[QuestionAnswers​​];
            }
        }
        组
        {
            会话[QuestionAnswers​​] =值;
        }
    }

在我的第一个控制器(从BaseController派生):

  QuestionAnswers = qaRepository.GetQuestionAnswers(CurrentSection,UserSmartFormID);

我在code加强和上面的语句执行罚款,设置会话[QuestionAnswers​​],但后来当我试图从下面另一个控制器,会话[QuestionAnswers​​]得到的是空<! / p>

我的第二个控制器(也BaseController派生):

 列表&LT;&问题答案GT; currentList =(列表&LT;&问题答案GT;)QuestionAnswers;

以上线路出现故障!它看起来像Session对象本身为空(不只是会话[QuestionAnswers​​])


解决方案

好吧,终于得到了它的工作,虽然有点缺憾。我发现从其他相关的SO职位的解决方案。

我添加了下面我BaseController:

 公开新HttpContextBase的HttpContext
    {
        得到
        {
            HttpContextWrapper语境=
                新HttpContextWrapper(System.Web.HttpContext.Current);
            返回(HttpContextBase)范围内;
        }
    }

然后设置/检索使用HttpContext.Session我的Session变量和工作正常!

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

解决方案

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!

这篇关于如何在MVC 2.0会话状态的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆