Jaz*_*eff 2 c# asp.net session session-variables
我有一个奇怪的问题,基本上我有一个使用会话的购物车.当我使用IIS7部署网站时,一切看起来都很好.我在一台电脑上为会话添加了一个产品,它显示在我的购物篮中.当我从另一台PC访问该网站时,篮子里面有这个项目!! ??
我的理解是每个用户浏览器的会话实例是唯一的这是正确的吗?如果是这样,我怎么设法做到这一点?我知道它可能是一些愚蠢但我无法弄清楚,任何帮助非常感谢!
我的会话购物车代码如下
#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
Run Code Online (Sandbox Code Playgroud)
您将单个静态引用存储到单个全局ShoppingCart.
这是一个可怕的想法.
无论何时编写ShoppingCart.Instance,它总是返回静态构造函数中设置的原始值.
你需要摆脱单身并始终使用会话.