HttpCookieCollection.Add 与 HttpCookieCollection.Set - Request.Cookies 集合是否复制到 Response.Cookies 集合?
我只是想澄清这一点。
我知道如果我在之前的请求中设置了 cookie,它将显示在我的 Request.Cookies
集合中。
我想更新我现有的 Cookie。
我的 Request.Cookies
集合中的 Cookie 是否已复制到我的 Response.Cookies
集合中?我是否需要使用 Response.Cookies.Add()
添加具有相同密钥的新 Cookie,还是需要使用 Response.Cookies.Set()
?
I just want to clear this up.
I know that if I have set a cookie on a previous request, it will show up in my Request.Cookies
collection.
I want to update my existing Cookie.
Are the cookies from my Request.Cookies
collection already copied to my Response.Cookies
collection? Do I need to add a new cookie with the same key using Response.Cookies.Add()
, or do I need to use Response.Cookies.Set()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一个区别:
Response.Cookies.Add()
将允许设置重复的 cookie http://msdn.microsoft.com/en-us/library/system.web.httpcookiecollection.add.aspxResponse.Cookies.Set()
将首先检查以确保 cookie 不存在,以确保 cookie 是唯一的 http://msdn.microsoft.com/en-us/library/system.web.httpcookiecollection.set.aspx重复的 cookie 通常需要额外处理才能确定哪个是最新的。我不确定您是否希望在同一网站上有重复的 cookie,也许其他人可以举个例子
编辑:在您的情况下,您想要使用 set 因为您正在更新。
There is a difference:
Response.Cookies.Add()
will allow duplicate cookies to be set http://msdn.microsoft.com/en-us/library/system.web.httpcookiecollection.add.aspxResponse.Cookies.Set()
will make sure the cookie is unique by first checking to ensure the cookie doesn't exist http://msdn.microsoft.com/en-us/library/system.web.httpcookiecollection.set.aspxDuplicate cookies typically requires extra handling to determine which is the most recent. I'm not sure of a case when you would want duplicate cookies on the same site, maybe someone else can chime in with an example
Edit: In your case, you want to use set because you are updating.