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.

範例

下列範例會建立名為LastVisit的新 cookie, 將 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 都會以 HTTP 輸出資料流程傳送至Set-Cookie標頭中的用戶端。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 Cookie集合存取的集合包含由用戶端傳送至標頭中伺服器的cookie。HttpRequestThe collection accessed through the Cookies collection of HttpRequest contains cookies transmitted by the client to the server in the Cookie header. 透過的Cookies Set-Cookie集合存取的集合包含在伺服器上建立的新cookie,並將其傳送至標頭中的用戶端。HttpResponseThe 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.

當您使用HttpResponse.Cookies集合新增 cookie 之後, 即使尚未將回應傳送至用戶端, HttpRequest.Cookies該 cookie 仍會立即在集合中提供。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.

適用於