Monday, October 22, 2012

Cookie value is nulled when using Response.Cookies

I have the following code to check if Cookie with name "city" already exists in Response.Cookies collection:
 if (HttpContext.Current.Response.Cookies["city"]!=null)   
   return HttpUtility.UrlDecode(HttpContext.Current.Response.Cookies["city"].Value);  

But to my big surprise this code is not working in that way that i expected. This code resets Cookie value to NULL when i am trying to check if Cookie value is not NULL.

Here the code that works fine for me:
 if (HttpContext.Current.Response.Cookies.AllKeys.Contains("city"))  
   return HttpUtility.UrlDecode(HttpContext.Current.Response.Cookies["city"].Value);  


No comments:

Post a Comment