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