MVC 2.0 中的会话状态如何工作?

发布于 2024-09-11 20:48:27 字数 1393 浏览 5 评论 0原文

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

我想将一些逻辑分解到一个单独的类(即RulesController)中,我可以在其中执行某些检查等,但是当我尝试引用那里的会话时,它是空的。很明显,会话仅在特定控制器的上下文中保持有效,但是每个人都在做什么呢?

我想这很常见,您想在不同的控制器中共享某些“全局”变量,最佳实践是什么?

这是我的代码的一部分:

在我的 BaseController 类中:

    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;
        }
    }

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

QuestionAnswers = qaRepository.GetQuestionAnswers(CurrentSection, UserSmartFormID);

我单步执行代码,上面的语句执行得很好,设置了 Session["QuestionAnswers"],但是当我尝试从下面的另一个控制器获取,Session["QuestionAnswers"] 为空!

我的第二个控制器(也源自 BaseController):

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

上面的行失败了!看起来 Session 对象本身为空(不仅仅是 Session["QuestionAnswers"])

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

撩起发的微风 2024-09-18 20:48:27

如果您使用以下命令检索会话,会有什么不同吗

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

does it make a difference if you retrieve your session using

HttpContext.Current.Session("mySpecialSession")  ''# note this is VB, not C#
绝不服输 2024-09-18 20:48:27

我相信 TempData 会解决您的问题,它在会话中运行并在多个请求中持续存在,但是默认情况下,一旦您再次访问它,它就会清除存储的数据,如果这是一个问题,您可以告诉它保留新的信息添加了 Keep() 函数。

所以在你的情况下:
...
TempData["问题答案"] = qAnswers;
...

还有更多信息:
http://weblogs.asp.net/jacqueseloff/存档/2009/11/17/tempdata-improvements.aspx

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

滴情不沾 2024-09-18 20:48:27

您在哪里访问第二个控制器中的会话?会话对象在构造函数中不可用,因为它是在生命周期的后期注入的。

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.

南巷近海 2024-09-18 20:48:27

好吧,终于开始工作了,虽然有点笨拙。我从另一个相关的 SO 帖子中找到了解决方案。

我将以下内容添加到我的 BaseController:

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

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

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文