HttpCookieCollection.Add(HttpCookie) 方法

定义

将指定的 Cookie 添加到此 Cookie 集合中。Adds the specified cookie to the cookie collection.

public:
 void Add(System::Web::HttpCookie ^ cookie);
public void Add (System.Web.HttpCookie cookie);
member this.Add : System.Web.HttpCookie -> unit
Public Sub Add (cookie As HttpCookie)

参数

cookie
HttpCookie

要添加到集合中的 HttpCookieThe HttpCookie to add to the collection.

示例

下面的示例创建一个新的 cookie 集合和一个名为 "LastVisit" 的新 cookie,将该 cookie 的值设置为当前日期和时间,并将该 cookie 添加到新集合中。The following example creates a new cookie collection and a new cookie named "LastVisit", sets the value of the cookie to the current date and time, and adds the cookie to the new collection.

HttpCookieCollection MyCookieCollection = new HttpCookieCollection();
 HttpCookie MyCookie = new HttpCookie("LastVisit");
 MyCookie.Value = DateTime.Now.ToString();
 MyCookieCollection.Add(MyCookie);

Dim MyCookieCollection as New HttpCookieCollection()
 Dim MyCookie As New HttpCookie("LastVisit")
 MyCookie.Value = DateTime.Now().ToString()
 MyCookieCollection.Add(MyCookie)
    

注解

应用程序中可以存在任意数量的 cookie 集合,但仅将内部对象引用的集合 Cookies 发送到客户端。Any number of cookie collections can exist within an application, but only the collection referenced by the intrinsic Cookies object is sent to the client.

Add方法允许 cookie 集合中存在重复的 cookie。The Add method allows duplicate cookies in the cookie collection. 使用 Set 方法可确保 cookie 集合中 cookie 的唯一性。Use the Set method to ensure the uniqueness of cookies in the cookie collection.

适用于

另请参阅