HttpResponse.Cookies 属性

定义

获取响应 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

属性值

HttpCookieCollection

响应 cookie 集合。The response cookie collection.

示例

下面的示例创建一个名为的新 cookie,将该 cookie 的 LastVisit 值设置为当前日期和时间,并将该 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 包含客户端向标头中的服务器传输的 cookie CookieThe collection accessed through the Cookies collection of HttpRequest contains cookies transmitted by the client to the server in the Cookie header. 通过的集合访问的集合 Cookies HttpResponse 包含在服务器上创建并在标头中传输到客户端的新 cookie Set-CookieThe 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.

适用于