views:

229

answers:

2

Hi!

In ASP.NET MVC, how can I create a single cookie and save multplie values to it?

Thanks!!

+5  A: 
var cookie = new HttpCookie();
cookie.Values["FirstKey"] = value1;
cookie.Values["SecondKey"] = value2;

or a shortcut:

cookie["ThirdKey"] = value3;

HttpCookie.Values is a NameValueCollection, a class that appears in many places in ASP.NET MVC. Read this for a good tutorial on how to use it to its full potential.

Talljoe
Thanks Talljoe! :-)
AndreMiranda
+1  A: 

This problem can be rephrased as: "How can I store multiple values in a single string?"

It's up to you to come up with a system. If the values are simples numbers or strings, use a separator like a ";" and then split later.

x0n
or use the other guy's idea. much better :D (upvoted him)
x0n