Listing 64 Displaying a Cookie That Contains Multiple Values ch6l aspxcs

void showCookie() {

HttpCookie aCookie; int i;

String[] keys;

HttpCookieCollection cookies = Request.Cookies; aCookie = cookies.Get("Items"); if (aCookie.HasKeys) {

Response.Write("This cookie has " +

aCookie.Values.AllKeys.Length.ToString() + " keys.<br>"); keys = aCookie.Values.AllKeys; for (i = 0; i < keys.Length; i++) {

Response.Write(keys[i] + "=" + aCookie.Values[i] + "<br>");

Response.Write("The cookie 'Items' has no keys.");

Response.Write("Cookie string value=" + aCookie.Value + "<br>");

The subkeys are actually stored like querystring parameters: An ampersand separates each key/value pair, and an equal sign separates the key from the value. The HttpCookie object parses this string into a collection when you access the subkeys via the values property. However, if you simply request the Value property of the cookie itself, you get a string constructed like this:

Cookie value=Items&1=Item1&2=Item2&3=Item3&4=Item4&5=Item5&6=Item6& 7=Item7&8=Item8&9=Item9

That's a rather contrived example. Here's a better one. Suppose you visit an e-commerce site that has a shopping cart. The site chose to implement the shopping cart as a set of in-memory cookies. Therefore, when a user selects an item, the application needs to add it to the cart. When a user elects to view the cart or check out, the application needs to read and display the values.

Although you can do this with standard function calls, a better method might be to create a ShoppingCart class. The class needs three methods:

■ DeleteFromCart

■ GetCartContents

In classic ASP, you had to use a GetObjectContext() method to get references to the ASP intrinsic objects from a COM class. In ASP.NET, you can simply reference the HttpContext.Current property to gain access to the current context. The HttpContext object exposes the set of ASP.NET objects associated with the current request; among those are the Request and Response objects, which you need to access the current request context from the ShoppingCart class. The ShoppingCart class takes advantage of .NET's capability to ferret out the current context. Each time you create an instance of the class, it creates a private class variable called context and sets its value to the current HttpContext object. By doing this, the class has complete access to the System.Web objects it needs to service the request. Of course, you could pass the required objects explicitly as parameters.

public class ShoppingCart { private HttpContext context; private HttpCookie aCookie;

public ShoppingCart() {

context = HttpContext.Current;

aCookie = context.Request.Cookies.Get("cart"); if (aCookie == null) {

aCookie = new HttpCookie("cart", "cart");

The class also attempts to retrieve the cookie called cart. Retrieving a cookie that doesn't exist does not raise an error—it simply sets the receiving variable to Null (or leaves it set to Null). Therefore, the New method in the class checks to see if the variable has been initialized; if not, it creates a new cookie.

You need a Web Form to consume the class. Figure 6.2 shows a simple Web Form that lets you add and delete items from ShoppingCart. In the figure, some items have already been added to the cart.

Figure 6.2: A simple Web Form to illustrate the ShoppingCart class methods

Figure 6.2: A simple Web Form to illustrate the ShoppingCart class methods

Listing 6.5 shows the complete code for the ShoppingCart class. Note that the class adds all values to the same key— cart. The AddToCart method uses the Response.Cookies.Values.Add method.

Was this article helpful?

0 0

Post a comment