检查cookie存在 [英] Check if Cookie Exists
问题描述
从上快速搜索的堆栈溢出的我看到人们暗示的检查方式如下如果cookie存在:
From a quick search on Stack Overflow I saw people suggesting the following way of checking if a cookie exists:
HttpContext.Current.Response.Cookies["cookie_name"] != null
或(在页
类):
this.Response.Cookies["cookie_name"] != null
然而,当我尝试使用索引(或Cookies.Get法)来检索一个cookie不存在,似乎真正的创建默认的cookie的名称和返回如此,这样无论我用什么cookie的名称它永远不会返回null。 (甚至更糟 - 创建不必要的cookie)
However, when I try to use the indexer (or the Cookies.Get method) to retrieve a cookie that does not exist it seems to actually create a 'default' cookie with that name and return that, thus no matter what cookie name I use it never returns null. (and even worse - creates an unwanted cookie)
难道我在这里做得不对,或者是有名字的特定的Cookie是否存在等检查只是以不同的方式?
Am I doing something wrong here, or is there a different way of simply checking for the existance of a specific cookie by name?
推荐答案
Response.Cookies包含将被发送回浏览器的cookie。如果你想知道一个Cookie是否存在,你应该看看Request.Cookies时。
Response.Cookies contains the cookies that will be sent back to the browser. If you want to know whether a cookie exists, you should probably look into Request.Cookies.
总之,要查看是否有cookie存在,你可以检查的 Cookies.Get(串)。 然而,如果你使用Response对象这个方法和饼干的不的存在,那么该cookie将被创建。
Anyway, to see if a cookie exists, you can check Cookies.Get(string). However, if you use this method on the Response object and the cookie doesn't exist, then that cookie will be created.
请参见 HttpCookieCollection.Get方法MSDN参考(字符串)
这篇关于检查cookie存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!