Check if Cookie Exists

后端 未结 7 560
失恋的感觉
失恋的感觉 2020-12-03 02:21

From a quick search on Stack Overflow I saw people suggesting the following way of checking if a cookie exists:

HttpContext.Current.Response.Cookies         


        
7条回答
  •  清歌不尽
    2020-12-03 02:58

    Sorry, not enough rep to add a comment, but from zmbq's answer:

    Anyway, to see if a cookie exists, you can check Cookies.Get(string), this will not modify the cookie collection.

    is maybe not fully correct, as Cookies.Get(string) will actually create a cookie with that name, if it does not already exist. However, as he said, you need to be looking at Request.Cookies, not Response.Cookies So, something like:

    bool cookieExists = HttpContext.Current.Request.Cookies["cookie_name"] != null;
    

提交回复
热议问题