我用cookie做购物车功能,在本地时可以正常使用,没出现问题
但放在空间后就出现以下问题了:
一、刷新购物车页面时,中文会出现乱码,刚增加商品到购物车时没有这个问题。
二、有时商品数量的变量会出现Value was either too large or too small for an Int32的错误,而且我的数量绝对没有超过Int32的范围,例如只有20时就会出现这个错误,而在本地测试时非常正常部分代码如下:
//往购物车中添加商品
HttpCookie cart = null;
if (Request.Cookies["ShoppingCart"] == null)
{
//如果Cookies中不存在ShoppingCart,则创建
cart = new HttpCookie("ShoppingCart");
}
else
{
//如果Cookies中存在ShoppingCart,则取出
cart = Request.Cookies["ShoppingCart"]; }
bool flag = true;//标记在购物车中是否存在本次选择的物品 //在购物车的Cookies中查找是否存在这次要选择的物品
foreach (string item in cart.Values)
{
if (item == id)
{
flag = false;
break;
}
}
if (flag)
{
//如果选择的内容在购物车中没有,则创建一个新的子键
cart.Values.Add(id, id + "|" + HttpUtility.UrlEncode(model.name) + "|" + model.price + "|" + 1 + "|" + model.number + "|" + model.small_img + "|" + model.price + "|");
}
else
{
//如果选择的内容在购物车中有,则删除原来的,添加一个新的
int num = int.Parse(cart.Values[id].Split(new char[] { '|' })[3]) + 1;
cart.Values.Remove(id);
cart.Values.Add(id, id + "|" + HttpUtility.UrlEncode(model.name) + "|" + model.price + "|" + num + "|" + model.number + "|" + model.small_img + "|" + Convert.ToInt32(model.price) * num + "|");
}
cart.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cart);
List<social.Common.ShoppingCart> list = new List<social.Common.ShoppingCart>();
//循环从购物车中取出物品添加到集合
if (Request.Cookies["ShoppingCart"] != null)
{
foreach (string item in Request.Cookies["ShoppingCart"].Values)
{
if (item != null)
{
char[] sp = { '|' }; string[] w = Request.Cookies["ShoppingCart"][item].Split(sp); social.Common.ShoppingCart gwc = new social.Common.ShoppingCart();
gwc.Id = w[0];
gwc.Name = HttpUtility.UrlDecode(w[1]);
gwc.Price = int.Parse(w[2]);
gwc.Number = int.Parse(w[3]); //商品数量
gwc.No = w[4]; //商品编号
gwc.Small_Img = w[5];
gwc.Small_Total=w[6];
list.Add(gwc);
}
}
}
return list;
string name; public string Name
{
get { return name; }
set { name = value; }
}
double price; public double Price
{
get { return price; }
set { price = value; }
}
string id; public string Id //id
{
get { return id; }
set { id = value; }
}
int number; public int Number //商品数量
{
get { return number; }
set { number = value; }
} string _img;
public string Img
{
set { _img = value; }
get { return _img; }
} string _total;
public string Total //总计
{
set { _total = value; }
get { return _total; }
} string _no;
public string No //商品编号
{
set { _no = value; }
get { return _no; }
} string _big_img;
public string Big_Img
{
set { _big_img = value; }
get { return _big_img; }
} string _small_img;
public string Small_Img
{
set { _small_img = value; }
get { return _small_img; }
} string _small_total;
public string Small_Total //小计
{
set { _small_total = value; }
get { return _small_total; }
}
但放在空间后就出现以下问题了:
一、刷新购物车页面时,中文会出现乱码,刚增加商品到购物车时没有这个问题。
二、有时商品数量的变量会出现Value was either too large or too small for an Int32的错误,而且我的数量绝对没有超过Int32的范围,例如只有20时就会出现这个错误,而在本地测试时非常正常部分代码如下:
//往购物车中添加商品
HttpCookie cart = null;
if (Request.Cookies["ShoppingCart"] == null)
{
//如果Cookies中不存在ShoppingCart,则创建
cart = new HttpCookie("ShoppingCart");
}
else
{
//如果Cookies中存在ShoppingCart,则取出
cart = Request.Cookies["ShoppingCart"]; }
bool flag = true;//标记在购物车中是否存在本次选择的物品 //在购物车的Cookies中查找是否存在这次要选择的物品
foreach (string item in cart.Values)
{
if (item == id)
{
flag = false;
break;
}
}
if (flag)
{
//如果选择的内容在购物车中没有,则创建一个新的子键
cart.Values.Add(id, id + "|" + HttpUtility.UrlEncode(model.name) + "|" + model.price + "|" + 1 + "|" + model.number + "|" + model.small_img + "|" + model.price + "|");
}
else
{
//如果选择的内容在购物车中有,则删除原来的,添加一个新的
int num = int.Parse(cart.Values[id].Split(new char[] { '|' })[3]) + 1;
cart.Values.Remove(id);
cart.Values.Add(id, id + "|" + HttpUtility.UrlEncode(model.name) + "|" + model.price + "|" + num + "|" + model.number + "|" + model.small_img + "|" + Convert.ToInt32(model.price) * num + "|");
}
cart.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cart);
List<social.Common.ShoppingCart> list = new List<social.Common.ShoppingCart>();
//循环从购物车中取出物品添加到集合
if (Request.Cookies["ShoppingCart"] != null)
{
foreach (string item in Request.Cookies["ShoppingCart"].Values)
{
if (item != null)
{
char[] sp = { '|' }; string[] w = Request.Cookies["ShoppingCart"][item].Split(sp); social.Common.ShoppingCart gwc = new social.Common.ShoppingCart();
gwc.Id = w[0];
gwc.Name = HttpUtility.UrlDecode(w[1]);
gwc.Price = int.Parse(w[2]);
gwc.Number = int.Parse(w[3]); //商品数量
gwc.No = w[4]; //商品编号
gwc.Small_Img = w[5];
gwc.Small_Total=w[6];
list.Add(gwc);
}
}
}
return list;
string name; public string Name
{
get { return name; }
set { name = value; }
}
double price; public double Price
{
get { return price; }
set { price = value; }
}
string id; public string Id //id
{
get { return id; }
set { id = value; }
}
int number; public int Number //商品数量
{
get { return number; }
set { number = value; }
} string _img;
public string Img
{
set { _img = value; }
get { return _img; }
} string _total;
public string Total //总计
{
set { _total = value; }
get { return _total; }
} string _no;
public string No //商品编号
{
set { _no = value; }
get { return _no; }
} string _big_img;
public string Big_Img
{
set { _big_img = value; }
get { return _big_img; }
} string _small_img;
public string Small_Img
{
set { _small_img = value; }
get { return _small_img; }
} string _small_total;
public string Small_Total //小计
{
set { _small_total = value; }
get { return _small_total; }
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货