Wednesday, May 30, 2018

ASP.NET MVC how to disable Caching

ASP.NET MVC Output Cache enables you to cache the action result content by a MVC controller action. By default MVC Output cache is enabled. So for this you don’t need to do anything, if client will send same request, browser will not forward request to MVC controller and it returns previous cached result but in few scenario, you don’t want to cache controller action result and want to disabled output caching for specific controller action.

You can use [OutputCache] attribute to disabled or enabled the browser caching for specific action or all action in controller.

·         Action Level: 
Disable output caching for specific action only

public class HomeController : Controller

{
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]

public ActionResult Index(ParametersData parameters)

{

   return View();

}
}

·         Controller Level:
Disable output caching for all actions in controller

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public class HomeController : Controller

{
public ActionResult Index(ParametersData parameters)

{

   return View();

}
}

·         Global/Application Level
You can a add a action filer in FilterConfig.cs for disabling caching for application controllers

public class FilterConfig

    {

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)

        {

            filters.Add(new CustomHandleErrorAttribute());

            filters.Add(new OutputCacheAttribute

            {

                VaryByParam = "*",

                Duration = 0,

                NoStore = true,

            });

        }

    }

Other ASP.NET MVC related topics:
Thanks for visiting!! 

2 comments:

SAM_Review said...

Hire asp.net developers for all your web applications development, CMS customisations and migrating your existing apps to the .NET Platform seamlessly now.

Hire Web and Mobile App Developer said...

Thanks for sharing this informative article on how to disable Caching in Asp Dot Net development with useful examples. If you have any requirement to Hire ASP.Net Developers for your web development solution. Please contact us.

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...