I'm setting a cookie in my controller and expecting to read that cookies new value when the page loads. However, the value that is printed is the previously set value. So assuming there is a cookie with value of test1, when the controller loads, the value
printed by the below response.write is "test1", even though I've set it to "test2". When I refresh the page, the value "test2" is then displayed.
The second part of your reply works, but doesn't ensure accuracy. I am useing the cookie to create a count for the number of times a user has invoked a certain type of route. So the cookie might be [mywebsite][pagetype]. the value for the pagetype key would
be a number, which is incremented when the route is invoked.
The issue is that the cookie may fail to write. So I've assumed that the cookie was succesfully persisted when I use the count, but I won't know as I'm not reading the actual value. I'm taking the old value, applying a code side increment, then useing that
value rather than the new cookie value.
So again, why am I seeing the old value when after I've written the new value?
you are confusing Request cookies (the ones the browser sent to the controller) and Response cookies (the new cookie values the controller is sending back to the browser).
I'm not sure I follow. I'm useing response to set the cookie and request to read it. Are you saying that is incorrect?
I believe the issue is that the request has alreday occured - when the page loaded. So any change I make to the cookie will not be available until the page is refreshed - e.g. the server gets a another request from the browser, thereby giving the new value
back to the server.
jypelton
Member
186 Points
162 Posts
Cookie not updated until refresh
Apr 20, 2012 07:43 PM|LINK
I'm setting a cookie in my controller and expecting to read that cookies new value when the page loads. However, the value that is printed is the previously set value. So assuming there is a cookie with value of test1, when the controller loads, the value printed by the below response.write is "test1", even though I've set it to "test2". When I refresh the page, the value "test2" is then displayed.
Response.Cookies["mycookies"]["categories"] = "test2";
Response.Write(Server.HtmlEncode(Request.Cookies["mycookies"]["categories"]));
I would like to set the value and then immediately read it.
ignatandrei
All-Star
137682 Points
22147 Posts
Moderator
MVP
Re: Cookie not updated until refresh
Apr 21, 2012 03:17 AM|LINK
My standard approach with array cookie was to read the whole array and to put again into the cookie.
You can read from the same value in the action
var test = "test2";
Response.Cookies["mycookies"]["categories"] = "test2";
//use test var instead of Request.Cookies["mycookies"]["categories"]
jypelton
Member
186 Points
162 Posts
Re: Cookie not updated until refresh
Apr 23, 2012 01:44 PM|LINK
Thanks ignatandrei
The second part of your reply works, but doesn't ensure accuracy. I am useing the cookie to create a count for the number of times a user has invoked a certain type of route. So the cookie might be [mywebsite][pagetype]. the value for the pagetype key would be a number, which is incremented when the route is invoked.
The issue is that the cookie may fail to write. So I've assumed that the cookie was succesfully persisted when I use the count, but I won't know as I'm not reading the actual value. I'm taking the old value, applying a code side increment, then useing that value rather than the new cookie value.
So again, why am I seeing the old value when after I've written the new value?
bruce (sqlwo...
All-Star
37614 Points
5573 Posts
Re: Cookie not updated until refresh
Apr 23, 2012 03:39 PM|LINK
you are confusing Request cookies (the ones the browser sent to the controller) and Response cookies (the new cookie values the controller is sending back to the browser).
jypelton
Member
186 Points
162 Posts
Re: Cookie not updated until refresh
Apr 23, 2012 04:34 PM|LINK
Thank you bruce.
I'm not sure I follow. I'm useing response to set the cookie and request to read it. Are you saying that is incorrect?
I believe the issue is that the request has alreday occured - when the page loaded. So any change I make to the cookie will not be available until the page is refreshed - e.g. the server gets a another request from the browser, thereby giving the new value back to the server.