Asp.Net MVC Creating Cache Profile using Web.Config

As an alternative to configuring output cache properties using OutputCache attribute in mvc controller method, you can create a cache profile in the web configuration file.

Two advantages of creating a cache profile in the web.config

First, By configuring output caching in the web configuration file, you can control it on one central location. You can create one cache profile and apply the profile to several controllers or controller actions.

Secondly, If we need to disable caching for an application that has been deployed on to production server, then we can simply modify the cache profiles defined in the web.config file. And any changes to the web configuration file will be applied automatically.

In the following code you can see I have used a new attribute CacheProfile that maps to Cache10Seconds that is in web.config.  

[OutputCache(CacheProfile = "Cache10Seconds", VaryByCustom = "LoggedUserName")] 
public ActionResult Profiles() 


 And add in the web.config file.

 <add name="Cache10Seconds" duration="10" varyByParam="none"/> 

Back to top