Editar

Compartilhar via


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.

public:
 System::Web::HttpCookie ^ Get(int index);
public System.Web.HttpCookie Get (int index);
member this.Get : int -> System.Web.HttpCookie
Public Function Get (index As Integer) As HttpCookie

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.

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);
    }
 }
Dim loop1 As Integer
 Dim MyCookie As HttpCookie
 Dim MyCookieCollection As HttpCookieCollection = Request.Cookies
 
 For loop1 = 0 To MyCookieCollection.Count - 1
    MyCookie = MyCookieCollection.Get(loop1)
    If MyCookie.Name = "LastVisit" Then
       MyCookie.Value = DateTime.Now().ToString()
       MyCookieCollection.Set(MyCookie)
    End If
 Next loop1

See also

Applies to

Get(String)

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

public:
 System::Web::HttpCookie ^ Get(System::String ^ name);
public System.Web.HttpCookie Get (string name);
member this.Get : string -> System.Web.HttpCookie
Public Function Get (name As String) As HttpCookie

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.

HttpCookieCollection MyCookieCollection = Request.Cookies;
 HttpCookie MyCookie = MyCookieCollection.Get("LastVisit");
 MyCookie.Value = DateTime.Now.ToString();
 MyCookieCollection.Set(MyCookie);
Dim MyCookieCollection As HttpCookieCollection = Request.Cookies
 Dim MyCookie As HttpCookie = 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