我想存储多个键值对。这是在单击按钮时第一次使用的代码
Dictionary<string, string> Dic_get_Cook = new Dictionary<string, string>();
Dic_get_Cook.Add("MyId" + itemId, "true");
Dic_get_Cook.Add("MyAge", item["Age"].ToString());
SetMultipleCookies("MyCookieName", Dic_get_Cook, cookie);
public void SetMultipleCookies(string cookieName, Dictionary<string, string> dic, HttpCookie cookie)
{
foreach (KeyValuePair<string, string> val in dic)
{
cookie[val.Key] = val.Value;
}
cookie.Expires = DateTime.Now.AddDays(30);
GetHttpResponse().Cookies.Add(cookie);
}
public static HttpResponse GetHttpResponse()
{
return HttpContext.Current.Response;
}
但是当我再次点击按钮时,它会出现错误An item with same key is already been added
答案 0 :(得分:0)
你几乎就在那里。使用HttpCookie.Values集合。
public void SetMultipleCookies(string cookieName, Dictionary<string, string> dic, HttpCookie cookie)
{
foreach (KeyValuePair<string, string> val in dic)
{
cookie.Values.Add(val.Key, val.Value);
}
cookie.Expires = DateTime.Now.AddDays(30);
HttpContext.Current.Response.Cookies.Add(cookie);
}