我有一个奇怪的问题,基本上我有一个使用 session 的购物车。当我使用 IIS7 部署站点时,一切看起来都很好。我在一台电脑上将产品添加到 session 中,它显示在我的购物篮中。当我从另一台电脑访问网站时,篮子里有这个项目!??
我的理解是每个用户浏览器的 session 实例都是唯一的,对吗?如果是这样,我是怎么做到的?我知道这可能有些愚蠢,但我想不通,非常感谢任何帮助!
我的 session 购物车代码如下
#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
最佳答案
您正在存储对单个全局 ShoppingCart
的单个静态引用。
这是一个可怕的想法。
每当您编写 ShoppingCart.Instance
时,它总是返回您在静态构造函数中设置的原始值。
您需要摆脱单例并始终使用 session 。
关于c# - session 在其他计算机上可见吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18340184/