カートを維持するために以下のコードを使用しています。セッションでエラーが発生し、ウェブサイトをブラウザよりも多く開いているときに、あるブラウザからアイテムを選択してから別のブラウザでアイテムを選択するとセッションの競合が発生するため、以前は作成されたセッションは更新されましたが、ブラウザごとに新しいセッションが必要ですが、セッションのエラーの領域を理解するのを手伝ってください。
#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