I've stumbled upon behavior that I cannot understand in ASP.net's handling of the Response.Cookies object.
The trigger for this post is a page where I have Response.Flush() followed a bit later with an attempt to write an HttpCookie to the Response. Everything works fine when I try to write the cookie like so:
HttpContext.Current.Response.Cookies[name].Value = value;
But when I use this alternate method:
HttpCookie httpCookie = new HttpCookie(name, value);
HttpContext.Current.Response.Cookies.Set(httpCookie);
I get this exception thrown at me:
"Server cannot modify cookies after HTTP headers have been sent."
The unsettling part is that the second situation makes more sense to me than the first. Why? Because once Response.Flush() is called the HTTP headers have indeed been sent to the client and setting a cookie's value (which is done through HTTP Headers) should not be possible.
Why then is it okay to do this (set a cookie after HTTP Headers have been flushed to the client) if I'm using the indexer [] instead of the Set() method?
Your insight will be greatly appreciated :)