C# 在cookie中设置多个键值对

C# 在cookie中设置多个键值对,c#,cookies,C#,Cookies,我想存储多个键值对。下面是单击按钮时第一次工作的代码 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", Di

我想存储多个键值对。下面是单击按钮时第一次工作的代码

 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;
    }
Dictionary Dic_get_Cook=new Dictionary();
添加(“MyId”+itemId,“true”);
Dic_get_Cook.Add(“MyAge”,item[“Age”].ToString());
设置多重书签(“mycokiename”,Dic_get_Cook,cookie);
public void SetMultipleCookies(字符串cookieName、字典dic、HttpCookie cookie)
{
foreach(dic中的KeyValuePair val)
{
cookie[val.Key]=值;
}
cookie.Expires=DateTime.Now.AddDays(30);
GetHttpResponse().Cookies.Add(cookie);
}
公共静态HttpResponse GetHttpResponse()
{
返回HttpContext.Current.Response;
}

但当我再次单击按钮时,它会给出错误
一个具有相同键的项目已经被添加了

您就快到了。使用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);
}
public void SetMultipleCookies(字符串cookieName、字典dic、HttpCookie cookie)
{
foreach(dic中的KeyValuePair val)
{
cookie.Values.Add(val.Key,val.Value);
}
cookie.Expires=DateTime.Now.AddDays(30);
HttpContext.Current.Response.Cookies.Add(cookie);
}

你就快到了。使用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);
}
public void SetMultipleCookies(字符串cookieName、字典dic、HttpCookie cookie)
{
foreach(dic中的KeyValuePair val)
{
cookie.Values.Add(val.Key,val.Value);
}
cookie.Expires=DateTime.Now.AddDays(30);
HttpContext.Current.Response.Cookies.Add(cookie);
}