共用方式為


HttpCookie.HasKeys 屬性

定義

取得值,指出 Cookie 是否有子索引鍵 (Subkey)。

public:
 property bool HasKeys { bool get(); };
public bool HasKeys { get; }
member this.HasKeys : bool
Public ReadOnly Property HasKeys As Boolean

屬性值

如果 Cookie 有子機碼 (Subkey),則為 true,否則為 false。 預設值是 false

範例

下列程式碼範例會檢查 Cookie 集合的每個成員是否有多個值。 如果 Cookie 的 HasKeys 屬性為 true ,表示存在多個值,本範例會將值名稱複製到一個字串陣列,並將對應的值複製到另一個字串陣列。 如需如何為 Cookie 建立多個值的範例,請參閱 Values

HttpCookieCollection MyCookieCollection = Request.Cookies;
 for(int loop1 = 0; loop1 < MyCookieCollection.Count; loop1++)
 {
    HttpCookie MyCookie = MyCookieCollection[loop1];

    if ( MyCookie.HasKeys )
    {
      NameValueCollection MyCookieValues =
          new NameValueCollection(MyCookie.Values);
      String[] MyKeyNames = MyCookieValues.AllKeys;
      foreach(string KeyName in MyKeyNames)
          {
              String[] MyValues =
                  MyCookieValues.GetValues(KeyName);
          }
    }
 }

Dim MyCookieCollection As HttpCookieCollection
Dim MyCookie As HttpCookie
Dim MyKeyNames() As String
Dim MyValues() As String
Dim loop1 As Integer

MyCookieCollection = Request.Cookies
For loop1 = 0 To MyCookieCollection.Count - 1
    MyCookie = MyCookieCollection(loop1)
    If MyCookie.HasKeys Then
        Dim MyCookieValues As NameValueCollection = _
            New NameValueCollection(MyCookie.Values)
        MyKeyNames = MyCookieValues.AllKeys
        For Each KeyName As String In MyKeyNames
            MyValues = MyCookieValues.GetValues(KeyName)
        Next
    End If
Next loop1

適用於

另請參閱