【问题标题】:HttpCookieCollection.Add vs HttpCookieCollection.Set - Does the Request.Cookies collection get copied to the Response.Cookies collection?HttpCookieCollection.Add 与 HttpCookieCollection.Set - Request.Cookies 集合是否被复制到 Response.Cookies 集合中?
【发布时间】:2011-04-01 18:23:54
【问题描述】:

我只是想澄清一下。

我知道,如果我在之前的请求中设置了 cookie,它将显示在我的 Request.Cookies 集合中。

我想更新我现有的 Cookie。

我的 Request.Cookies 集合中的 cookie 是否已复制到我的 Response.Cookies 集合中?我需要使用Response.Cookies.Add() 添加具有相同密钥的新cookie,还是需要使用Response.Cookies.Set()

【问题讨论】:

    标签: asp.net httprequest httpresponse httpcookie httpcookiecollection


    【解决方案1】:

    有区别:

    重复的 cookie 通常需要额外处理才能确定哪个是最新的。我不确定您是否希望在同一站点上重复 cookie,也许其他人可以举个例子

    编辑:在你的情况下,你想使用 set 因为你正在更新。

    【讨论】:

    • 谢谢 - 我也做了一些思考,我意识到我问错了问题。请查看此帖子:stackoverflow.com/questions/5517595/…
    • 这没有很好的记录。 original documentation 包含语句“更新 cookie 集合中 现有 cookie 的值”,这是错误的,因为如果 cookie 不存在,则会添加它。 underlying code 使用 BaseAdd 调用更新现有密钥并添加新密钥。
    • 我不认为有人可以确信该文档在 2011 年引用的代码与您链接到的 4.7.2 代码相同。
    猜你喜欢
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 2010-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-13
    相关资源
    最近更新 更多