简体   繁体   中英

MVC: Add output cache for all actions

One of my MVC project is close to the end. 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.

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.

Use global filters in FilterConfig.cs

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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