所有用户的静态 asp.net 会话

发布于 2024-12-10 06:24:46 字数 804 浏览 0 评论 0原文

我正在开发一个带有购物车的 ASP.net 4.0 应用程序。问题是用户 1 放置了一些东西,而用户 2 也得到了它。怎么可能是会话共享...

// 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["ASPNETShoppingCart"] == null) {
        Instance = new ShoppingCart();
        Instance.Items = new List<CartItem>();
        HttpContext.Current.Session["ASPNETShoppingCart"] = Instance;
    } else {
        Instance = (ShoppingCart)HttpContext.Current.Session["ASPNETShoppingCart"];
    }
}

请帮忙,我找不到解决方案...

I developing a ASP.net 4.0 application, with a shoppingcart. The problem is that user 1 places something, and user 2 gets it also. How can it been session sharing...

// 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["ASPNETShoppingCart"] == null) {
        Instance = new ShoppingCart();
        Instance.Items = new List<CartItem>();
        HttpContext.Current.Session["ASPNETShoppingCart"] = Instance;
    } else {
        Instance = (ShoppingCart)HttpContext.Current.Session["ASPNETShoppingCart"];
    }
}

Please help, i can't find a solution....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我纯我任性 2024-12-17 06:24:46

不,不要使用这个静态变量:

public static readonly ShoppingCart Instance;

像这样替换它:

public static ShoppingCart Instance
{
    get
    {
        if (HttpContext.Current.Session["ASPNETShoppingCart"] == null) {
            // we are creating a local variable and thus
            // not interfering with other users sessions
            ShoppingCart instance = new ShoppingCart();
            instance.Items = new List<CartItem>();
            HttpContext.Current.Session["ASPNETShoppingCart"] = instance;
            return instance;
        } else {
            // we are returning the shopping cart for the given user
            return (ShoppingCart)HttpContext.Current.Session["ASPNETShoppingCart"];
        }
    }
}

No, don't use this static variable:

public static readonly ShoppingCart Instance;

Replace it like this:

public static ShoppingCart Instance
{
    get
    {
        if (HttpContext.Current.Session["ASPNETShoppingCart"] == null) {
            // we are creating a local variable and thus
            // not interfering with other users sessions
            ShoppingCart instance = new ShoppingCart();
            instance.Items = new List<CartItem>();
            HttpContext.Current.Session["ASPNETShoppingCart"] = instance;
            return instance;
        } else {
            // we are returning the shopping cart for the given user
            return (ShoppingCart)HttpContext.Current.Session["ASPNETShoppingCart"];
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文