1

Below is the code in JAVA written inside a controller. I am saving the cart object inside the HttpSession, so that I can retrieve it always for the same session. Is there any way to do a similar thing in C#?

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println("in servlet");
    Cart cart = getCartFromSession(request);
}

Cart getCartFromSession(HttpServletRequest req){
    HttpSession session = req.getSession(true);
    Cart cart=(Cart)session.getAttribute("cart");
    if(cart==null){
        cart = new Cart();
        session.setAttribute("cart", cart);
    }
    return cart;
}
4

1 回答 1

10

Use HttpContext.Session Property

To Save:

Session["cart"] = cart;

To Retrieve:

Cart cart = Session["cart"] as Cart;
if(cart != null)
{
   //found
}

See: ASP.NET Session State Overview

于 2013-11-04T14:46:33.713 回答