use*_*119 1 c# java asp.net session-state httpsession
下面是在控制器内部编写的JAVA代码.我将购物车对象保存在HttpSession中,以便我可以为同一个会话检索它.有没有办法在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;
}
Run Code Online (Sandbox Code Playgroud)
保存:
Session["cart"] = cart;
Run Code Online (Sandbox Code Playgroud)
要检索:
Cart cart = Session["cart"] as Cart;
if(cart != null)
{
//found
}
Run Code Online (Sandbox Code Playgroud)
请参阅:ASP.NET会话状态概述