ASP.NET MVC会话 [英] ASP.NET MVC Session

查看:150
本文介绍了ASP.NET MVC会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读Steven Sanderson撰写的PRO ASP.NET MVC2,但仍然对会话一无所知. 在书中,他讲述了如何使用自定义模型绑定程序基于会话来开发Cart来持久化会话.一切正常,但我不知道它到底是如何工作的. 由于代码量很大,所以我将编写一个简化的版本

I've been reading PRO ASP.NET MVC2 by Steven Sanderson and I still can't figure out something about session. In the book he tells how to develop a Cart based on session using a Custom model binder for session persisting. Everything works fine but I can't figure out how it really works under the hood. Since it's a fair amount of code I'll write a simplified version

计数器

public class Counter
{
    public int counter = 0;

    public void Increment(){
        counter++;
    }
}

CounterController

public ActionResult Index(Counter counter)
{
   counter.Increment();
   return View(counter);
}

CounterCustomModelBinder

public class CounterCustomModelBinder: IModelBinder
{
    private const string counterSessionKey = "_counter";

    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        Counter counter = (Counter)controllerContext.HttpContext.Session[counterSessionKey];
        if (counter == null)
        {
            counter = new Counter();
            controllerContext.HttpContext.Session[counterSessionKey] = counter;
        }
        return counter;
    }
}

Global.asax

...
ModelBinders.Binders.Add(typeof(Counter), new CounterCustomModelBinder());

如您所见,其中有一条用于获取会话内容的语句 计数器counter =(Counter)controllerContext.HttpContext.Session [counterSessionKey]; 但是没有任何声明可以保存到会话中.我希望随后的声明在某处: controllerContext.HttpContext.Session [counterSessionKey] =计数器; 但是此代码不会出现在任何地方

As you see there's a statement for fetching the session contents Counter counter = (Counter)controllerContext.HttpContext.Session[counterSessionKey]; But there's no statement for SAVING into session. I would expect the subsequent statement somewhere: controllerContext.HttpContext.Session[counterSessionKey] = counter; But this code doesn't appear anywhere

尽管如此,它仍然有效. 以某种方式更新Counter对象时,会话会自动进行更新...但是我不知道何时何地. 多亏了任何人都会回复.

Nevertheless it still works. Somehow when updating the Counter object, the session gets updated Automagically... But I can't understand where when and HOW. Thanks to anyone will reply.

推荐答案

计数器对象是一个类(引用类型),因此计数器变量引用了Session对象中已经存在的值.请参见 http://www.albahari.com/valuevsreftypes.aspx .

The counter object is a class (reference type), and so the value that is already in the Session object is referenced by the the counter variable. See http://www.albahari.com/valuevsreftypes.aspx.

更新计数器对象中的值时,将在会话中已经存在的副本中更新它们.

When you update the values in the counter object, you are updating them in the copy that already exists in the Session.

请注意,如果您使用会话服务器或SQL会话服务器,这可能将不起作用,因为它们对会话执行不同的操作.现在,您正在使用一个实时的Session值集合,这就是为什么它可以工作的原因.在两次请求之间,该对象在ASP.NET的进程中保持活动状态.

Note that this will probably not work if you use a Session server or a SQL Session server, as they do different things with Session. Right now, you are using a single, live collection of Session values, which is why it works at all. The object is being kept alive in ASP.NET's process between requests.

这篇关于ASP.NET MVC会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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