通过快速搜索Stack Overflow,我看到人们建议使用以下方法检查cookie是否存在:
HttpContext.Current.Response.Cookies["cookie_name"] != null
Run Code Online (Sandbox Code Playgroud)
或(在Page课堂内):
this.Response.Cookies["cookie_name"] != null
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用索引器(或Cookies.Get方法)来检索不存在的cookie时,似乎实际上创建了一个具有该名称的"默认"cookie并返回该cookie,因此无论我使用什么cookie名称它永远不会返回null.(更糟糕的是 - 创建一个不需要的cookie)
我在这里做错了什么,或者是否有一种不同的方式来简单地通过名称检查特定cookie的存在?
我在我的代码中多次使用这两个并且不知道区别是什么,如果设置了cookie,它在请求和响应中是否应该完全相同?请求是最新的还是回复?
编辑:
好吧,我得到了请求和响应之间的区别,但如果我输入
string a = HttpContext.Current.Request.Cookie["a"].Value;
Run Code Online (Sandbox Code Playgroud)
它大部分时间都一样
string a = HttpContext.Current.Response.Cookie["a"].Value;
Run Code Online (Sandbox Code Playgroud)
但我想知道使用这两者有什么区别.