HttpCookieCollection.Add vs HttpCookieCollection.Set - Does the Request.Cookies collection get copied to the Response.Cookies collection?

霸气de小男生 提交于 2019-12-17 13:45:23

问题


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()?


回答1:


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.aspx
  • Response.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.aspx

Duplicate 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.



来源:https://stackoverflow.com/questions/5517273/httpcookiecollection-add-vs-httpcookiecollection-set-does-the-request-cookies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!