HttpResponse.Cookies HttpResponse.Cookies HttpResponse.Cookies HttpResponse.Cookies Property

定義

取得回應 Cookie 集合。Gets the response cookie collection.

public:
 property System::Web::HttpCookieCollection ^ Cookies { System::Web::HttpCookieCollection ^ get(); };
public System.Web.HttpCookieCollection Cookies { get; }
member this.Cookies : System.Web.HttpCookieCollection
Public ReadOnly Property Cookies As HttpCookieCollection

屬性值

回應 Cookie 集合。The response cookie collection.

範例

下列範例會建立新的 cookie,名為LastVisit管理員、 設定 cookie 的值為目前的日期和時間,並將 cookie 加入至目前的 cookie 集合。The following example creates a new cookie named LastVisit, sets the value of the cookie to the current date and time, and adds the cookie to the current cookie collection. 中的用戶端傳送 cookie 集合中的所有 cookie Set-Cookie HTTP 標頭輸出資料流。All cookies in the cookie collection are sent to the client in the Set-Cookie header with the HTTP output stream.

HttpCookie MyCookie = new HttpCookie("LastVisit");
DateTime now = DateTime.Now;

MyCookie.Value = now.ToString();
MyCookie.Expires = now.AddHours(1);

Response.Cookies.Add(MyCookie);
   
Dim MyCookie As New HttpCookie("LastVisit")
Dim now As DateTime = DateTime.Now

MyCookie.Value = now.ToString()
MyCookie.Expires = now.AddHours(1)

Response.Cookies.Add(MyCookie)
   

備註

ASP.NET 包含兩個內建 cookie 集合。ASP.NET includes two intrinsic cookie collections. 透過存取的集合Cookies的集合HttpRequest包含傳輸中的伺服器的用戶端的 cookieCookie標頭。The collection accessed through the Cookies collection of HttpRequest contains cookies transmitted by the client to the server in the Cookie header. 透過存取的集合Cookies的集合HttpResponse包含在伺服器上建立並傳輸至用戶端中的新 cookieSet-Cookie標頭。The collection accessed through the Cookies collection of HttpResponse contains new cookies created on the server and transmitted to the client in the Set-Cookie header.

使用新增 cookie 之後HttpResponse.Cookies集合中,cookie 可立即用於HttpRequest.Cookies集合,即使尚未傳送回應給用戶端。After you add a cookie by using the HttpResponse.Cookies collection, the cookie is immediately available in the HttpRequest.Cookies collection, even if the response has not been sent to the client.

適用於