Thursday, March 16, 2006

Cookies in ASP.NET

Cookie collections in ASP.NET are just weird to say the least. I'm too tired to go into details. Just remember this:
  1. When you read Response.Cookies[somename], the cookie is automatically created and returned if it does not already exist. Code which checks if(Response.Cookies[somename] == null) is pointless.
    (This does not apply to Request.Cookies)
  2. When a cookie is added to Response.Cookies, it is automatically added to Request.Cookies too. Ditto for deletes.
  3. Want more? If you add a cookie to Response.Cookies and it already exists in Request.Cookies, then Request.Cookies will have two cookies with the same name. So it is better to do a Request.Cookies.Remove(somename) before Response.Cookies[somename].
    For cookies using multiple values it makes sense to reuse the cookie which was removed from Request.Cookies.
  4. If you'd like even more bugs with your ASP.NET: Response.Cookies Bug
Yup. That was the short version.

1 comment:

Anonymous said...

Cookies, like session are created when u assign a value to the cookies. You may have to check
if(Request.Cookies!=null) when u are accessing the cookie value.

About Me

My photo
C/C++ Programmer doing CAD on Windows. Some web development experience. Bangalorean.

Blog Archive

Labels