我正在使用下面的代码来维护购物车。会话中有一个错误,当我打开我的网站而不是浏览器时,当我从一个浏览器中选择项目然后到另一个浏览器时,会出现会话冲突,所以以前创建的会话已更新,尽管每个浏览器都必须有新的会话请有人帮助我了解会话中错误的区域。
#region Singleton Implementation
// 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["ShoppingCart"] == null)
{
Instance = new ShoppingCart();
Instance.Items = new List<CartItem>();
HttpContext.Current.Session.Add("ShoppingCart", Instance);
}
else
{
Instance = (ShoppingCart)HttpContext.Current.Session["ShoppingCart"];
}
}
// A protected constructor ensures that an object can't be created from outside
protected ShoppingCart() { }
#endregion