2009-10-14 68 views
5

我正在使用ASP.NET和C#。具有多个值的ASP.NET cookie - 如何?

我必须阅读一个名为 “TheCookie” 饼干.............

TheCookie在其中具有约3的值。 Cookie1,Cookie2和Cookie3。

我如何获得代码中的值来读取“TheCookie”中Cookie2的值?

这是我如何阅读时,只有一个cookie的值,但我不知道该怎么办,当在cookie中有多个vales ..........代码为VB.NET

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

在此先感谢!

+0

你需要解释这furthur,我想。一个cookie只能有一个值。您可以设置多个Cookie或将单个Cookie值设置为具有多极属性的对象? – Sheff 2009-10-14 08:47:44

回答

12

您通过

(C#)

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

(VB)

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

设置它们,像这样

(C#)

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

(VB)

Dim myValue As String 
myValue = Request.Cookies("TheCookie")("Cookie1") 
2
Request.Cookies.Get("TheCookie").Values.Get("Cookie1") 
Request.Cookies.Get("TheCookie").Values.Get("Cookie2") 
Request.Cookies.Get("TheCookie").Values.Get("Cookie3") 

C#语法,对不起!

0

我们可以通过传递字典对象键值对来保存它,如下所示。

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