简体   繁体   English

MVC:为所有操作添加输出缓存

[英]MVC: Add output cache for all actions

One of my MVC project is close to the end. 我的MVC项目之一即将结束。 We are trying to optimize the project with Output Cache. 我们正在尝试使用输出缓存优化项目。

However, We found there are so many Controllers with even more Actions. 但是,我们发现有太多具有更多操作的控制器。 We do not think to add the Output Cache attribute to each Action is a good idea. 我们认为向每个Action添加Output Cache属性不是一个好主意。

Is there any solution that I could add Output Cache to each Action for one time? 有什么解决方案可以一次将输出缓存添加到每个操作中吗?

Add it to Global Filters. 将其添加到全局过滤器。

filters.Add(new OutputCacheAttribute 
 { 
    NoStore = true, 
    Duration = 0,
    VaryByParam = "*"
 });

You can do this in FilterConfig.cs file in App_Start folder. 您可以在App_Start文件夹的FilterConfig.cs文件中执行此App_Start

Use global filters in FilterConfig.cs 在FilterConfig.cs中使用全局过滤器

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {

                OutputCacheAttribute cache = new OutputCacheAttribute();
                  //set other properties
                filters.Add(cache);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM