HttpCookieCollection.Get Method

Definition

Returns an individual HttpCookie object from the cookie collection. This property is overloaded to allow retrieval of cookies by either name or numerical index.

Overloads

Get(Int32)

Returns the HttpCookie item with the specified index from the cookie collection.

Get(String)

Returns the cookie with the specified name from the cookie collection.

Get(Int32)

Returns the HttpCookie item with the specified index from the cookie collection.

C#
public System.Web.HttpCookie Get(int index);

Parameters

index
Int32

The index of the cookie to return from the collection.

Returns

The HttpCookie specified by index.

Examples

The following example returns each cookie from the cookie collection, checks whether it is named "LastVisit", and, if "LastVisit" is found, updates its value to the current date and time.

C#
int loop1;
 HttpCookie MyCookie;
 HttpCookieCollection MyCookieCollection = Response.Cookies;

 for(loop1 = 0; loop1 < MyCookieCollection.Count; loop1++)
 {
    MyCookie = MyCookieCollection.Get(loop1);
    if(MyCookie.Value == "LastVisit")
    {
       MyCookie.Value = DateTime.Now.ToString();
       MyCookieCollection.Set(MyCookie);
    }
 }

See also

Applies to

.NET Framework 4.8.1 i druge verzije
Proizvod Verzije
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Get(String)

Returns the cookie with the specified name from the cookie collection.

C#
public System.Web.HttpCookie Get(string name);

Parameters

name
String

The name of the cookie to retrieve from the collection.

Returns

The HttpCookie specified by name.

Examples

The following example captures the cookie collection sent by the client into a new cookie collection, retrieves the cookie named "LastVisit" from the new collection, and updates the cookie's value to the current date and time.

C#
HttpCookieCollection MyCookieCollection = Request.Cookies;
 HttpCookie MyCookie = MyCookieCollection.Get("LastVisit");
 MyCookie.Value = DateTime.Now.ToString();
 MyCookieCollection.Set(MyCookie);

Remarks

If the named cookie does not exist and the cookie collection is HttpResponse.Cookies, this method creates a new cookie with that name.

See also

Applies to

.NET Framework 4.8.1 i druge verzije
Proizvod Verzije
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1