mvcdonutcaching
mvcdonutcaching copied to clipboard
Handling Cookies
Firstly - MVC Donut Cache is excellent, thanks for releasing.
I'm having an issue understanding how cookies are meant to be handled with respect to the output cache.
Let's say I set a cookie in the method of a cached controller:
[DonutOutputCache(Duration = 43200, VaryByParam = "*")]
public ActionResult Index()
{
...
HttpCookie cookie = new HttpCookie("cookie");
cookie.Value = "cookieValue";
cookie.Expires = DateTime.Now.AddMonths(1);
cookie.Shareable = true;
Response.Cookies.Add(cookie);
...
Return View();
}
On the first load of this page, the cookie will be set. On all subsequent loads, it is not set.
Looking around a bit my understanding was setting cookie.Shareable to true would mean the cookie would be sent with all responses - is this not the case? Using DonutOutputCache, this setting appears to make no difference.
Do you have a different way to handle cookies in the response which works for all requests?
Thanks, Daz.
The cookie is not set for subsequent requests, this is the expected behaviour as your result will be cached for 43200 seconds upon first load. The body of your Action code won't be exectuted until expiration.
You should not set a cookie in a donut cached action (or anything that alters the response in headers terms).
Thanks for the reponse - I can see that is the case, I was just hoping that: cookie.Shareable = true; Could be used to ascertain whether a cookie should be cached or not in all requests that follow.
There is another workaround, which is to respond with a redirect to the same controller (adding a paramater of cookie set = true), redirects are ignored by the Donut Output Cache, and hence, the cookie will be set and the cached page loaded from the subsequent request.
Alternatively, I guess one could use javascript to set any cookies...
Ideally however, there would be a way to cache and replay the cookie response as part of the cache. I appreciate this may be out of scope for the project!
I was completely unaware of this property, Mvc's native OutputCache respects this property, so it's clear that MvcDonutCaching should too.
I'm very unfamiliar with your codebase - is it going to be a substantial change? Or something that can be offloaded to the native OutputCache?
If you point me in the right direction I'll happily see if I can implement.
@dazbradbury I also have this problem... I need to update a cookie, but the action is cached, so the cookie isn't changed. How did you solve this?
@migig I believe that is a different question all together, if you need to update the cookie you'll need your controller code (or a higher priority filter) to be executed. Lots of options here, action filter, controller with view that loads a cached controller / view action, etc.. This however is not related to this specific bug / feature.
@dazbradbury Library still doesn't support cookie.Shareable so you should'nt close this issue yet.
Are cookies supported now?
no @leen3o I'm sorry the ticket has been wrongly closed