asp.Netクッキーで簡単にカート機能を実現

1554 ワード

1、まずショッピングカートの模型類を創立します:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Ants.WebPart.Models
{
    public class ShopCarModel
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Other { get; set; }
    }
}

 
2.ショッピングカートの機能を実現する二つの方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Ants.WebPart.Models;
using Ants.Common;

namespace Ants.WebPart
{
    public class ShopCarTask
    {
        private static string _cookieName =TextUtils.ConvertToBasic64("MyshopcarPowerByAnts");
        Ants.Provider.ISerializerProvider serializer = new Ants.Provider.JsonSerializerProvider();
        public void SetShopCar(IList<ShopCarModel> goods)
        {
           CookieUtils.Write(_cookieName,TextUtils.ConvertToBasic64(serializer.ToString(goods)));
        }
        public void SetShopCar(IList<ShopCarModel> goods, int expires)
        {
            CookieUtils.Write(_cookieName, TextUtils.ConvertToBasic64(serializer.ToString(goods)),expires);
        }
        public IList<ShopCarModel> GetShopCar()
        {
           return serializer.parse < IList < ShopCarModel >>(CookieUtils.Read(_cookieName));
        }
    }
}