What the general way of storing custom objects in sessions?
I'm planning on keeping my cart in a session throughout the web application. When that user logs out, the session will be cleared.
Class ShoppingCart
{
private List<CartItem> Items = new List<CartItem>();
public ShoppingCart()
{
this.Items = new List<CartItem>();
if (HttpCurrent.Current["Cart"]!=null])
{
this.Items = ShoppingCart.loadCart(HttpCurrent.Current["User"]);
}
}
}
When the user signs in, I place the cart in a session, like:
Session["Cart"] = new ShoppingCart();
But do I have to write Session["Cart"]
on each and every page? Isn't there an easier way to do this? Also what about the Guest cart session? Where will I declare that?
I want each user session stored in a unique session, so that there's no mixing up between the guest session and the member session.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…