HttpCookieCollection.Get 方法

定义

从 Cookie 集合中返回单个 HttpCookie 对象。Returns an individual HttpCookie object from the cookie collection. 重载此属性以允许按名称或数字索引检索 Cookie。This property is overloaded to allow retrieval of cookies by either name or numerical index.

重载

Get(Int32)

从 Cookie 集合中返回具有指定索引的 HttpCookie 项。Returns the HttpCookie item with the specified index from the cookie collection.

Get(String)

从 Cookie 集合中返回具有指定名称的 Cookie。Returns the cookie with the specified name from the cookie collection.

Get(Int32)

从 Cookie 集合中返回具有指定索引的 HttpCookie 项。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

参数

index
Int32

要从集合中检索的 Cookie 索引。The index of the cookie to return from the collection.

返回

HttpCookie

HttpCookie 指定的 indexThe HttpCookie specified by index.

示例

下面的示例从 cookie 集合中返回每个 cookie,检查其是否命名为 "LastVisit",如果找到 "LastVisit",则将其值更新为当前日期和时间。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
   

另请参阅

适用于

Get(String)

从 Cookie 集合中返回具有指定名称的 Cookie。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

参数

name
String

要从集合中检索的 Cookie 的名称。The name of the cookie to retrieve from the collection.

返回

HttpCookie

HttpCookie 指定的 nameThe HttpCookie specified by name.

示例

下面的示例将客户端发送的 cookie 集合捕获到新的 cookie 集合,从新集合中检索名为 "LastVisit" 的 cookie,并将 cookie 的值更新为当前日期和时间。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)
   

注解

如果指定的 cookie 不存在并且 cookie 集合为,则 HttpResponse.Cookies 此方法将使用该名称创建一个新的 cookie。If the named cookie does not exist and the cookie collection is HttpResponse.Cookies, this method creates a new cookie with that name.

另请参阅

适用于