在操作上设置 OutputCache 属性时,是否有办法防止页面被缓存?
这样,当随后访问该页面时,它不会存储之前返回的通用错误页面。
下面的示例显示了一个实例,当应用程序因任何原因(数据库超时等)导致异常时,应用程序不希望缓存页面。
[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();
}
更新 最后,我只需要添加以下内容:
filterContext.HttpContext.Response.RemoveOutputCacheItem(filterContext.HttpContext.Request.Url.PathAndQuery);
这取自another question .
最佳答案
最简单的方法是不从此操作方法返回错误 View ,而是在错误发生时重定向到错误操作。
catch
{
return RedirectToAction("Error");
}
如果您无法做到这一点,可以编写一个操作过滤器来调整response.cache值。
关于asp.net-mvc - 防止发生错误时缓存返回的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3716334/