7/8/2016 - 8:34 PM

Caching data in ASP.NET MVC. Controller ActionResults and even the JSON Results can be cached.

Caching data in ASP.NET MVC. Controller ActionResults and even the JSON Results can be cached.

 //This prevents IE and Firefox from caching the results of action methods
 public class NoCache : ActionFilterAttribute
        public override void OnResultExecuting(ResultExecutingContext filterContext)

    Now on top of the Controller or the action method do
    public ActionResult MyActionMethod(){}
//Add a filterconfig.cs file in App_Start folder

  public class FilterConfig
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
            filters.Add(new HandleErrorAttribute());

            /*We don't want anything in this application cached in the server as most pages are dynamic and should go through
             * specific flow so that application will function smoothly
             *  */
            filters.Add(new OutputCacheAttribute
                VaryByParam = "*",
                Duration = 0,
                NoStore = true,
    //Now put the following line in Global.asax Application_Start method
     //This should disable caching in entire application
It can be done directly on the controller or the action methods or we can create CacheProfiles in web.config

            <add name="WebsiteCache" duration="3600" varyByParam="None" />

Now on top of controller or action methods, do

[OutputCache(CacheProfile = "WebsiteCache")]
Internet explorer and Firefox caches by default but chrome doesn't cache by default