我有一個奇怪的問題,基本上我有一個購物車使用會話。當我使用IIS7部署網站時,看起來很好。我在一臺電腦上將產品添加到會議中,並顯示在我的購物籃中。當我從另一臺電腦訪問網站時,籃子裏有這個物品!! ??會話在其他計算機上可見嗎?
其我的理解是,每個用戶瀏覽器的會話實例是唯一的,這是正確的嗎?如果是這樣,我怎麼設法做到這一點?我知道它可能是一些愚蠢的東西,但我無法弄清楚,任何幫助都非常感謝!
我會車的代碼如下
#region Singleton Implementation
public static readonly ShoppingCart Instance;
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["sCart"] == null)
{
Instance = new ShoppingCart();
Instance.Items = new List<CartItem>();
HttpContext.Current.Session["sCart"] = Instance;
}
else
{
Instance = (ShoppingCart)HttpContext.Current.Session["sCart"];
}
}
protected ShoppingCart() { }
#endregion
你的會話單例實現是錯誤的。見例如http://stackoverflow.com/questions/6076459/static-singleton-in-asp-net-session。您不得將引用存儲在靜態變量中,您應該只返回它。 – IvanH