Is there a way to to prevent a page from being cached when the OutputCache attribute has been set on an action?
This is so that, when the page is subsequently hit, it will not store the generic error page that was returned previously.
An example below shows an instance where it would be desirable for the application not to cache the page, when it causes an exception for whatever reason (db timeout, etc).
[OutputCache(CacheProfile = "Homepage")]
public ActionResult Index()
{
var model = new HomepageModel();
try
{
model = Db.GetHomepage();
}
catch
{
//Do not want to cache this!
return View("Error");
}
//Want to cache this!
return View();
}
Update In the end, I simply needed to add the following:
filterContext.HttpContext.Response.RemoveOutputCacheItem(filterContext.HttpContext.Request.Url.PathAndQuery);
This is taken from another question.