HttpCookieCollection.Get 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
回傳 cookie 集合中的單一 HttpCookie 物件。 此特性被過度載重,允許以名稱或數字索引檢索 Cookie。
多載
| 名稱 | Description |
|---|---|
| Get(Int32) |
回傳 HttpCookie cookie 集合中指定索引的項目。 |
| Get(String) |
回傳 cookie 集合中指定名稱的 cookie。 |
Get(Int32)
回傳 HttpCookie cookie 集合中指定索引的項目。
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 索引。
傳回
由 HttpCookieindex指定 。
範例
以下範例會回傳 Cookie 集合中的每個 Cookie,檢查其名稱是否為「LastVisit」,若找到「LastVisit」,則將其值更新為當前日期與時間。
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。
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 名稱。
傳回
由 HttpCookiename指定 。
範例
以下範例將客戶端傳送到新的 Cookie 集合中擷取,從新集合中取回名為「LastVisit」的 Cookie,並將 Cookie 的值更新為當前日期與時間。
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。