有一些不同的方法可以在 ASP.NET 中创建多值 cookie:
var cookie = new HttpCookie("MyCookie");
cookie["Information 1"] = "value 1";
cookie["Information 2"] = "value 2";
// first way
Response.Cookies.Add(cookie);
// second way
Response.AppendCookie(cookie);
// third way
Response.SetCookie(cookie);
什么时候应该使用哪种方式?我读过 SetCookie
方法更新 cookie,如果它已经存在的话。其他方式不会更新现有的 cookie 吗?
下面的代码是编写单值 cookie 的最佳实践吗?
Response.Cookies["MyCookie"].Value = "value";
最佳答案
如果我没记错的话
Response.Cookies.Add(..)
和
Response.AppendCookie(..)
将允许将多个同名的 cookie 附加到响应中。
另一方面
Response.SetCookie(..)
和
Response.Cookies[key].Value = value;
将始终覆盖以前同名的 cookie。
关于c# - HttpResponse : SetCookie, AppendCookie、Cookies.Add 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15889371/