gpt4 book ai didi

ASP.NET MVC 如何禁用自动缓存选项?

转载 作者:行者123 更新时间:2023-12-03 05:23:19 25 4
gpt4 key购买 nike

如何从 ASP.NET MVC 应用程序禁用自动浏览器缓存?

因为我在缓存方面遇到问题,因为它缓存了所有链接。但有时它会自动重定向到默认索引页它存储了它的缓存,然后每次我点击该链接,它都会将我重定向到默认索引页面。

那么有人知道如何从 ASP.NET MVC 4 手动禁用缓存选项吗?

最佳答案

您可以使用 OutputCacheAttribute 来控制服务器和/或浏览器缓存特定操作或 Controller 中的所有操作。

禁用 Controller 中的所有操作

[OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)] // will be applied to all actions in MyController, unless those actions override with their own decoration
public class MyController : Controller
{
// ...
}

禁用特定操作:

public class MyController : Controller
{
[OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)] // will disable caching for Index only
public ActionResult Index()
{
return View();
}
}

如果您想对所有 Controller 中的所有操作应用默认缓存策略,您可以添加 global action filter通过编辑 global.asax.cs 并查找 RegisterGlobalFilters 方法。该方法已添加到默认的 MVC 应用程序项目模板中。

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new OutputCacheAttribute
{
VaryByParam = "*",
Duration = 0,
NoStore = true,
});
// the rest of your global filters here
}

这将导致它将上面指定的 OutputCacheAttribute 应用于每个操作,这将禁用服务器和浏览器缓存。您仍然应该能够通过将 OutputCacheAttribute 添加到特定操作和 Controller 来覆盖此无缓存。

关于ASP.NET MVC 如何禁用自动缓存选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12948156/

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