ASP.NET MVC 如何禁用自动缓存选项? [英] ASP.NET MVC how to disable automatic caching option?

查看:28
本文介绍了ASP.NET MVC 如何禁用自动缓存选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 asp.Net mvc 应用程序禁用自动浏览器缓存?

How to disable automatic browser caching from asp.Net mvc application?

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

Because I am having a problem with caching as it caches all links. But sometimes it redirected to DEFAULT INDEX PAGE automatically which stored it caching and then all the time I click to that link it will redirect me to DEFAULT INDEX PAGE.

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

So some one know how to manually disable caching option from ASP.NET MVC 4?

推荐答案

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

You can use the OutputCacheAttribute to control server and/or browser caching for specific actions or all actions in a controller.

禁用控制器中的所有操作

Disable for all actions in a 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();
    }
} 

如果要对所有控制器中的所有操作应用默认缓存策略,可以添加 全局操作过滤器 通过编辑您的 global.asax.cs 并查找 RegisterGlobalFilters 方法.这个方法是在默认的MVC应用项目模板中添加的.

If you want to apply a default caching strategy to all actions in all controllers, you can add a global action filter by editing your global.asax.cs and looking for the RegisterGlobalFilters method. This method is added in the default MVC application project template.

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

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

This will cause it to apply the OutputCacheAttribute specified above to every action, which will disable server and browser caching. You should still be able to override this no-cache by adding OutputCacheAttribute to specific actions and controllers.

这篇关于ASP.NET MVC 如何禁用自动缓存选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆