Introduce the Problem
We have successfully configured the browser cache to return a saved response if the server indicates 304 Not Modified
.
The following is working for us.
Set Cache-Control:private,max-age-0
to enable caching and force re-validation.
Respond with 304 if the response is not modified.
[MyOutputCache(CacheProfile="TransparentClient")]
public ActionResult ValidateMe()
{
// check whether the response is modified
// replace this with some ETag or Last-Modified comparison
bool isModified = DateTime.Now.Second < 30;
if (isModified)
{
return View();
}
else
{
return new HttpStatusCodeResult(304, "Not Modified");
}
}
Remove the Cache-Control:private,max-age-0
otherwise the cache will store the status message.
public class MyOutputCache : OutputCacheAttribute
{
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
base.OnResultExecuted(filterContext);
if (filterContext.HttpContext.Response.StatusCode == 304)
{
// do not cache the 304 response
filterContext.HttpContext.Response.CacheControl = "";
}
}
}
Fiddler shows that the cache is acting appropriately.