www.gibmonks.com

Main Page

  Previous Section Next Section

17.3 Collections Reference

The Response object in ASP.NET supports only a single collection, the Cookies collection.

Cookies

HttpCookieCollection = Response.Cookies

The Cookies collection returns an instance of the HttpCookieCollection class containing all cookies sent as a part of the current request. The HttpCookieCollection class contains an instance of the HttpCookie class for each cookie passed as part of the client request. The properties of these HttpCookie instances can be used to access information about the cookie(s). The Cookies collection of the Response class supports the following set of properties:

AllKeys

Returns a string array of all keys in the collection.

Count

Returns an integer count of the number of name/value pairs in the collection.

Item(Index|Key)

Returns an instance of the collection class based on the index or passed-in key. This is the default property, which is why calling:

Response.Cookies (KeyVal)

returns the HttpCookie instance corresponding to KeyVal.

Keys

Returns a collection of the keys for the collection.

In addition, the HttpCookieCollection class exposes the following methods:

CopyTo(Array, Index)

Copies the contents of the collection object to the provided Array argument, starting at the provided Index argument. Note that the array must be dimensioned to a sufficient size to contain the collection before calling CopyTo.

GetKey(Index)

Returns a string containing the key corresponding to the provided Index argument.

As in classic ASP, the Cookies collection is still implemented as a collection (in fact, the HttpCookieCollection class inherits from the .NET NameObjectCollectionBase class), but rather than a collection of string keys and string values, the ASP.NET implementation is a collection of string keys and objects (instances of the HttpCookie class). Individual cookies are retrieved into variables of type HttpCookie, providing access to the cookies values through class properties.

Dictionary-style cookies (cookies with more than one value) are accessible through the Values property of the HttpCookie class, which returns a NameValueCollection containing the cookie subkeys and values. You can also set individual values by their key with the following syntax:

HttpCookie.Values("keyname") = "value"

Parameters

HttpCookieCollection

An Object variable of type HttpCookieCollection.

Example

The example creates a login cookie, sets the expiration of the cookie for 30 minutes from the current time, and adds the cookie to the Cookies collection.

Sub Page_Load( )
   Dim myCookie As New HttpCookie("LoggedIn")
   myCookie.Value = "True"
   myCookie.Expires = DateTime.Now.AddMinutes(30)
   Response.Cookies.Add(myCookie)
End Sub

Notes

Unlike classic ASP, the collections in ASP.NET are zero-based, so the first element in any collection or array will be 0, not 1. This is especially important to remember when retrieving values by their index.

      Previous Section Next Section