HttpResponse.Cookies 屬性

定義

取得回應 Cookie 集合。

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

屬性值

HttpCookieCollection

回應 Cookie 集合。

範例

下列範例會建立名為 LastVisit 的新 Cookie,將 Cookie 的值設定為目前的日期和時間,並將 Cookie 新增至目前的 Cookie 集合。 Cookie 集合中的所有 Cookie 都會傳送至標頭中具有 HTTP 輸出資料流程的 Set-Cookie 用戶端。

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 集合。 透過 集合 HttpRequest 存取的 Cookies 集合包含用戶端傳送至標頭中伺服器所傳輸的 Cookie Cookie。 透過 集合 HttpResponse 存取的 Cookies 集合包含伺服器上建立的新 Cookie,並傳輸至標頭中的 Set-Cookie 用戶端。

使用 HttpResponse.Cookies 集合新增 Cookie 之後,即使尚未將回應傳送至用戶端,該 Cookie 仍可立即在集合中使用 HttpRequest.Cookies

適用於