Check if Cookie Exists

后端 未结 7 479
失恋的感觉
失恋的感觉 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 03:04
    public static class CookieHelper
    {
        /// <summary>
        /// Checks whether a cookie exists.
        /// </summary>
        /// <param name="cookieCollection">A CookieCollection, such as Response.Cookies.</param>
        /// <param name="name">The cookie name to delete.</param>
        /// <returns>A bool indicating whether a cookie exists.</returns>
        public static bool Exists(this HttpCookieCollection cookieCollection, string name)
        {
            if (cookieCollection == null)
            {
                throw new ArgumentNullException("cookieCollection");
            }
    
            return cookieCollection[name] != null;
        }
    }
    

    Usage:

    Request.Cookies.Exists("MyCookie")
    
    0 讨论(0)
提交回复
热议问题