gpt4 book ai didi

asp.net-mvc - 防止发生错误时缓存返回的 View

转载 作者:行者123 更新时间:2023-12-04 06:46:25 24 4
gpt4 key购买 nike

在操作上设置了 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 .

最佳答案

最简单的方法是不从这个 Action 方法返回错误 View ,而是在发生错误时重定向到一个错误 Action 。

catch
{
return RedirectToAction("Error");
}

如果您不能这样做,则可以编写一个调整 response.cache 值的操作过滤器。

关于asp.net-mvc - 防止发生错误时缓存返回的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3716334/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com