ASP.NET购物车Cookie获取,创建,添加,更新,删除的用法

#region 添加购物车
public void GetShoppingCart(int skuId, int quanlity)
{

  HttpCookie cookie = HttpContext.Current.Request.Cookies["myCart"];//获取cookie

     var reg = Session["userInfo"] as Registration; 用户登入状态

  #region  未登入状态时

     if(reg == null)

  {

    if(cookie == null) //cookie为null时

           {

      cookie = new HttpCookie("myCart");//创建cookie 

                 cookie.Values.Add(skuId.ToString(), quanlity.ToString());//添加Cookie

                 cookie.Expires = DateTime.Today.AddDays(30);//设置Cookie的过期日期或时间

      Response.Cookies.Add(cookie);//将指定的cookie添加到此Cookie集合中

           }

          else if(cookie != null)

          {

      if(cookie.Values.AllKeys.Contains(skuId.ToString()))//判断cookie中是否存在此键

                 {

        cookie.Values.Add(skuId.ToString(), quanlity.ToString());

        cookie.Expires = DateTime.Today.AddDays(30);

                      Response.Cookies.Add(cookie);

                 }

                 else

                 {

        cookie.Values[skuId.ToString()] = (Int32.Parse(cookie.Values[skuId.ToString()]) + quanlity).ToString();

        cookie.Expires = DateTime.Today.AddDays(30);

        Response.Cookies.Add(cookie);  

                 }

          }

     }

     #endregion

}

#endregion

 

 

#region 更新购物车
public void UpdateShoppingCart(int skuId, int quanlity)
{

  HttpCookie cookie = HttpContext.Current.Request.Cookies["myCart"];

  Sku skuOne = CommodityInfoManager.GetSkuOneBySkuId(skuId.ToString());

     var reg = Session["userInfo"] as Registration;

     if(reg != null)//登入用户账号时,直接修改数据库中的购物车的数量

     {

     ShoppingCarInfoManager.UpdateShoppingCartBySkuIdAndRegisterId(skuId, reg.RegisterId, quanlity); 

     }

     else

     {

    cookie.Values[skuId.ToString()] = quanlity.ToString();//更新cookie中某一项的对应的cookie的值

           cookie.Expires = DateTime.Today.AddDays(30);

           Response.Cookies.Add(cookie);

     }

}

#endregion

 

#region  删除购物车某一个商品

public void DeleteSkuId(int skuId, out int cookiState)
{

  cookiState = 1;

      HttpCookie cookie = HttpContext.Current.Request.Cookies["myCart"];

      var reg = Session["userInfo"] as Registration; 

  #region 登录账号
  if (reg != null)
  {
    ShoppingCart shoppingCart = ShoppingCarInfoManager.GetShoppingCartById(skuId, reg.RegisterId);
    if (shoppingCart != null)
    {
    ShoppingCarInfoManager.DeletedShoppingCartBySkuIdAndRegisertId(skuId, reg.RegisterId);
    }
  }
  #endregion

  #region 未登录账号 

  else
  {

    if(cookie != null)

           {

      if(cookie.Values.AllKeys.Length != 1)//判断cookie里有很多的键值对

                 {

        if(cookie.Values.AllKeys.Contains(skuId.ToString()))

        {

          cookie.Values.Remove(skuId.ToString());//移除cookie中指定的某一项

          cookie.Expires = DateTime.Today.AddDays(30);

          Response.Cookies.Add(cookie);

                      }

                 }

      else

      {

        cookie.Expires = DateTime.Now.AddDays(-1);//清除cookie  

        cookiState = 0;

          Response.Cookies.Add(cookie);  

                 }

           }

     }

  #endregion 

}

#endregion

 

#region 清空购物车
public void DeletedShoppingCart(out int cookiState)
{
  cookiState = 1;
  HttpCookie cookie = HttpContext.Current.Request.Cookies["myCart"];
  var reg = Session["userInfo"] as Registration;
  if (reg != null)//清除购物车
  {
    List<ShoppingCart> shoppingCarts =                                ShoppingCarInfoManager.GetShoppingCartsByRegisterId(reg.RegisterId);
    foreach (var r in shoppingCarts)
    {
      ShoppingCarInfoManager.DeletedShoppingCartBySkuIdAndRegisertId(r.SkuId, reg.RegisterId);
    }
  }
  else //清除cookie
  {
    if (cookie != null)
    {
      cookie.Expires = DateTime.Now.AddDays(-1);
      cookiState = 0;
      Response.Cookies.Add(cookie);
    }
  }
}
#endregion

ASP.NET购物车Cookie获取,创建,添加,更新,删除的用法,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。