【问题标题】:classic asp.net shoppingcart session state经典的 asp.net 购物车会话状态
【发布时间】:2011-08-16 00:49:42
【问题描述】:

我用这个例子来创建一个购物车: http://net.tutsplus.com/tutorials/other/build-a-shopping-cart-in-aspnet/

这是一个很好的例子,它将购物车存储在 Session["cart"] 状态,它应该可以正常工作。

但事实并非如此。事件如果关闭浏览器,或者尝试不同的浏览器,它仍然保持状态?!?!

这里是构造函数+添加到购物车方法:

public List<CartItem> Items { get; private set; }

        // Readonly properties can only be set in initialization or in a constructor
        public static readonly ShoppingCart Instance;
        // The static constructor is called as soon as the class is loaded into memory
        static ShoppingCart()
        {
            // If the cart is not in the session, create one and put it there
            // Otherwise, get it from the session
            if (HttpContext.Current.Session["MPBooksCart"] == null)
            {
                Instance = new ShoppingCart();
                Instance.Items = new List<CartItem>();
                HttpContext.Current.Session["MPBooksCart"] = Instance;
            }
            else
            {
                Instance = (ShoppingCart)HttpContext.Current.Session["MPBooksCart"];
            }
        }
        // A protected constructor ensures that an object can't be created from outside
        protected ShoppingCart() { }

        public void AddItem(int book_id)
        {
            // Create a new item to add to the cart
            CartItem newItem = new CartItem(book_id);
            // If this item already exists in our list of items, increase the quantity
            // Otherwise, add the new item to the list
            if (this.Items.Contains(newItem))
            {
                foreach (CartItem i in Items)
                {
                    if (i.Equals(newItem))
                    {
                        i.Quantity++;
                        return;
                    }
                }
            }
            else
            {
                newItem.Quantity = 1;
                Items.Add(newItem);
            }

        }

能否请您告知问题可能是什么?

关于会话状态,我已经阅读了大约 2 个小时,并且到处都说关闭 broser 时它应该是 volatile 的,但在这种情况下它不是。

问候, 亚历克斯

【问题讨论】:

    标签: c# asp.net session-state shopping-cart


    【解决方案1】:

    我不太确定使用单例模式来保存会话实例。如果您考虑一下,会话对于每个用户和访问该网站的每个浏览器都必须是唯一的。单例模式创建一个全局唯一实例。我不知道你做了多少 asp.net 但是,以防你对 asp.net 相当陌生,会话对于特定的浏览器实例来说是唯一的。这意味着访问 Session["MPBooksCart"] 的每个浏览器都将访问他们自己唯一的数据副本。默认情况下,asp.net 会话将保存其数据 20 分钟(这可以在数据库中进行配置)。 如果我正在编写购物车,我可能会直接使用数据库中的购物车表和 caritems 表。 Rob Connery 的MVC Samples App 就是一个很好的店面网站示例。这是一个 ASP.Net MVC 应用程序,所以如果您不熟悉 MVC,您可能会发现这有点难以理解。

    【讨论】:

    • 您好安德鲁,感谢您的回复。我对 ASP.net 相当陌生。我认为单例也是这个问题。这只是我正在构建的图书网站的预览,您可以自己查看此版本不起作用。如果您单击图书价格下的购买链接,它将执行 ajax 进程来更新会话状态并将总项目放在白色菜单上方。如果关闭浏览器,您将看到数字相同或更高。 mp-books.ru/html
    • @Alex,不幸的是,我的俄语远没有你的英语好。不过我喜欢你的设计。查看网上购物车的其他一些示例。我喜欢 ASP.Net webforms(经典)的一个是 Microsoft 的 .Net PetShop 4.0 示例应用程序。
    猜你喜欢
    • 1970-01-01
    • 2011-05-14
    • 2022-11-12
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    • 1970-01-01
    • 2018-03-01
    • 2011-08-18
    相关资源
    最近更新 更多