完整的用Cookies和HashTable制作购物车
今天在做个购物功能。采用Cookies和HashTable 作为购物车。
网上和园子里有很多现成代码:但是少了json类的源码故无法运行,真坑爹啊!
/// <summary>
/// 添加到购物车
/// </summary>
/// <param name="Primarykey">主键,内部以','形式分开</param>
/// <param name="Quantity">索引</param>
public static void AddCart(string Primarykey, int Quantity)
{
if (string.IsNullOrEmpty(GetCookie()))//如果cookies为空
{
Hashtable ht = new Hashtable();
ht.Add(Primarykey, Quantity);
AddCookies(Json.Serialize<Hashtable>(ht));
}
else
{
Hashtable ht = Json.Deserialize<Hashtable>(GetCookie());
if (ht.Contains(Primarykey))
{
Int32 s = (Int32)ht[Primarykey] + Quantity;
ht.Remove(Primarykey);
ht.Add(Primarykey, s);//更新其数量
}
else
{
ht.Add(Primarykey, Quantity);
}
AddCookies(Json.Serialize<Hashtable>(ht));
}
}
/// <summary>
/// 修改购物车的属性
/// </summary>
/// <param name="Primarykey"></param>
/// <param name="OldKey"></param>
public static void UpdateCart(string NewKey, string OldKey)
{
if (string.IsNullOrEmpty(GetCookie()))//如果cookies为空
{
}
else
{
Hashtable ht = Json.Deserialize<Hashtable>(GetCookie());
if (ht.Contains(OldKey))
{
Int32 s = (Int32)ht[OldKey];
ht.Remove(OldKey);
ht.Add(NewKey, s);//更新其数量
AddCookies(Json.Serialize<Hashtable>(ht));
}
}
}
/// <summary>
/// 更新购物车数量
/// </summary>
/// <param name="Key"></param>
/// <param name="Count"></param>
public static void UpdateCartCount(string Key, int Count)
{
if (string.IsNullOrEmpty(GetCookie()))//如果cookies为空
{
}
else
{
Hashtable ht = Json.Deserialize<Hashtable>(GetCookie());
if (ht.Contains(Key))
{
ht.Remove(Key);
ht.Add(Key, Count);//更新其数量
AddCookies(Json.Serialize<Hashtable>(ht));
}
}
}
/// <summary>
/// 获取cookies值
/// </summary>
public static Hashtable GetCart()
{
try
{
if (!string.IsNullOrEmpty(GetCookie()))
{
return Json.Deserialize<Hashtable>(GetCookie());
}
else
{
return null;
}
}
catch
{
return null;
}
//foreach (DictionaryEntry de in ht)
//{
// Console.WriteLine("Key is {0},Values is {1}", de.Key, de.Value);
//}
}
/// <summary>
/// 移除购物车
/// </summary>
/// <param name="Primarykey">主键 0为清理所有的购物车信息</param>
public static void ClearCart(string Primarykey)
{
if (Primarykey == "0")//为0的时候清理全部购物车cookies
{
HttpContext.Current.Request.Cookies["ChinahooCart"].Expires = DateTime.Now.AddHours(-1);
HttpContext.Current.Response.Cookies.Add(HttpContext.Current.Request.Cookies["ChinahooCart"]);
}
else
{
if (!string.IsNullOrEmpty(GetCookie()))//如果cookies为空
{
Hashtable ht = Json.Deserialize<Hashtable>(GetCookie());
if (ht.Contains(Primarykey))
{
ht.Remove(Primarykey);
}
AddCookies(Json.Serialize<Hashtable>(ht));
}
}
}
/// <summary>
/// 添加对象到cookies
/// </summary>
/// <param name="strValue"></param>
private static void AddCookies(string strValue)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies["ChinahooCart"];
if (cookie == null)
{
cookie = new HttpCookie("ChinahooCart");
}
cookie.Value = strValue;
cookie.Expires = DateTime.Now.AddYears(1);
HttpContext.Current.Response.AppendCookie(cookie);
}
/// <summary>
/// 获取cookies
/// </summary>
/// <returns></returns>
public static string GetCookie()
{
if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies["ChinahooCart"] != null)
return HttpContext.Current.Request.Cookies["ChinahooCart"].Value.ToString();
return null;
}
直接运行肯定不行。因为少了个自己实现的类了。
/// <summary>
/// JSON序列化和反序列化辅助类
/// </summary>
public class Json
{
/// <summary>
/// JSON序列化
/// </summary>
public static string Serialize<T>(T t)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream();
ser.WriteObject(ms, t);
string jsonString = Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
return jsonString;
}
/// <summary>
/// JSON反序列化
/// </summary>
public static T Deserialize<T>(string jsonString)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
T obj = (T)ser.ReadObject(ms);
return obj;
}
}