开发者

ASP.NET cookies with multiple values - how to?

I am using ASP.NET and C#.

I must read a cookie called "TheCookie".............

TheCookie have about 3 values in them. Cookie1, Cookie2 and Cookie3.

How would i get the value in code to read Cookie2's value inside "TheCookie"?

This is how i would read when a cookie only have 1 value, but i dont know what to do when there are multiple vales in the cookie..开发者_开发知识库........ Code for VB.NET

Dim userCookie As HttpCookie
userCookie = Request.Cookies("UserEmail")

Thanks in advance!


You set them via

(C#)

Response.Cookies["TheCookie"]["Cookie1"] = "Hello World";

(VB)

Response.Cookies("TheCookie")("Cookie1") = "Hello World"

and read them like so

(C#)

string myValue = Request.Cookies["TheCookie"]["Cookie1"];

(VB)

Dim myValue As String
myValue = Request.Cookies("TheCookie")("Cookie1")


Request.Cookies.Get("TheCookie").Values.Get("Cookie1")
Request.Cookies.Get("TheCookie").Values.Get("Cookie2")
Request.Cookies.Get("TheCookie").Values.Get("Cookie3")

C# syntax, sorry!


we can save it by passing as dictionary object key value pair as below.

 HttpCookie hc = new HttpCookie(cookieName);
 foreach (KeyValuePair<string, string> val in dic)
 {
   hc[val.Key] = val.Value;
  }
 hc.Expires = DateTime.Now.Add(TimeSpan.FromHours(20000));
 GetHttpResponse().Cookies.Add(hc);

Example

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜