HttpCookie.HasKeys 属性

定义

获取一个值,通过该值指示 Cookie 是否具有子键。

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

属性值

如果 Cookie 具有子键,则为 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

适用于

另请参阅