When to use Request.Cookies over Response.Cookies?

后端 未结 6 588
囚心锁ツ
囚心锁ツ 2020-12-02 07:30

Do I use response when at a page event (e.g. load) as this is a response from ASP.NET, and request when pressing a button as this is a response going to ASP.NET for processi

6条回答
  •  情书的邮戳
    2020-12-02 08:06

    Andrew's Code gave an error in "AllKeys.Contains" Method. So I corrected a little..

    public void WriteCookie(string strCookieName, string strCookieValue)
        {
            var hcCookie = new HttpCookie(strCookieName, strCookieValue);
            HttpContext.Current.Response.Cookies.Set(hcCookie);
        }
    
    
        public string ReadCookie(string strCookieName)
        {    
            foreach (string strCookie in HttpContext.Current.Response.Cookies.AllKeys)
            {
                if (strCookie == strCookieName)
                {
                    return HttpContext.Current.Response.Cookies[strCookie].Value;
                }
            }         
    
            foreach (string strCookie in HttpContext.Current.Request.Cookies.AllKeys)
            {
                if (strCookie == strCookieName)
                {
                    return HttpContext.Current.Request.Cookies[strCookie].Value;
                }
            }
    
            return null;
        }
    

提交回复
热议问题