Reputation: 11317
I have a very simple page with the following logic:
protected void Page_Load(object sender, EventArgs e)
{
if (null == Response.Cookies["UserSettings"].Value)
{
HttpCookie cookie = new HttpCookie("UserSettings");
cookie.Value = "The Big C";
cookie.Expires = DateTime.Now.AddDays(10);
Response.Cookies.Add(cookie);
}
else
{
// got here
}
}
I set a breakpoint in both the if
and the else
and the else
break point never gets hit. The if
statement gets hit every time. What could be wrong here?
Thanks!
Upvotes: 1
Views: 2643
Reputation: 18430
Why are you checking Response.Cookies
you should be checking Request.Cookies
. response is still being created..
Update
See, When you add a cookie by using the HttpResponse.Cookies collection, the cookie is immediately available in the HttpRequest.Cookies collection, even if the response has not been sent to the client. But you are checking for the cookie in the Collection even before its added. So yu need to check for it in Request.Cookie Collection
Upvotes: 5