经典的 ASP.NET 购物车会话状态

发布于 2024-11-04 13:54:10 字数 2201 浏览 0 评论 0原文

我用这个例子创建了一个购物车: 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 时它应该是不稳定的,但在本例中事实并非如此。

问候, 亚历克斯

I have used this example to create a shopping cart :
http://net.tutsplus.com/tutorials/other/build-a-shopping-cart-in-aspnet/

Its a good example, it stores the shoppingcart in the Session["cart"] state and it should all work fine.

BUT it doesn't. Event if close the browser, or try different browsers, it still maintains state?!?!

Here is the constructor + the add to cart method:

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

        }

May you please advise on what the issue might be?

I've read for about 2 hours regarding session state and everywhere it says it should be volatile when closing broser, but in this case it isn't.

Regards,
Alex

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

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

发布评论

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

评论(1

梦中楼上月下 2024-11-11 13:54:10

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

I am not so sure about using a Singleton pattern to hold an instance of a session. If you think about it the session will need to be unique for each user and each browser that accesses the website. The Singleton pattern creates a single global unique instance. I don't know how much asp.net you have done but, just in case you are fairly new to asp.net a session will be unique to a specific browser instance. That means that each browser accessing the Session["MPBooksCart"] will access their own unique copy of the data. By default an asp.net session will hold its data for 20 minutes (this can be configured in the database).
If I was writing a shopping cart I would probrably just work directly with a cart table and cartitems table in a database. A very good example of a storefront website is Rob Connery's MVC Samples App. This is an ASP.Net MVC application so if you aren't familiar with MVC you may find this a little bit hard to follow.

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