OutputCache explanation

Why should we use caching?

The reason why we should use caching is to increase the performance for the end user and to decrease the traffic on the server. There are multiple kind of caching. In this post, I will explain the OutputCache attribute of MVC 3 (and 4).

Using the OutputCache Attribute

To cache a page or image in ASP .NET MVC 3 we only have to set the OutputCache attribute above the action.


The Duration property is the timespan you want to cache the action in seconds. So above, the page will be cached for 10 seconds. When you run the application, the page is showing the ViewBag.Time on your screen. If you hit F5 (refresh), the page is refreshed but your action isn’t. The time is still the same if you did refresh the page within the 10 seconds. When you hit F5 after 10 seconds, the new time is shown.

Use fiddler to see if the caching is working.

Caching the action for different result

When you have an action for different results, like asking for an image for a given imageId you use a parameter in your action. The OutputCache attribute will automatically use the parameter for caching the result for that specific item.


So when you ask for example image 3 and 24, the caching will work on both images.

Use fiddler to see if the caching is working.

Caching of private items

By default, the cache is saved on the server and client. But when you have private data the cache should only be used on the client (the browser). Otherwise, private data could be accessed by anyone.

We could use the location property for this.


Set the location property to Client to only store the image in the browser.

When you run the page now and see the image or page, the image or page is cached only in your browser. When you hit F5, the caching won’t work. This is because client caching will not work for browser refreshing. If you use a hyperlink to the same page or image the caching will work.

Again use fiddler to see the results.

How to disable caching of an action

To disable caching for an action, use the code as shown below.


Caching profiles

When you have a lot of actions with the same settings, you could implement caching profiles. In this way, you only have to change the settings on one place.

In the web.config, add the following section.


You then could use the caching profile in the following way.


ASP .NET MVC 3 Release Candidate released

Ralph JansenOn November 9th 2010 the ASP .NET team released the release candidate of MVC 3. The big change of MVC 3 is the new “Razor” view engine. With this new view engine the syntax of the code is a lot smaller. You now just put an @ on your editor and the intellisense will pick it up.

An other thing that is added in MVC 3 is: partial caching of pages. You can add an attribute above your action in the controller with the right amount of seconds that you want to cache the information. You can even say what the dependent parameter is that the caching will focus on. So if an action is fired and the parametervalue is the same as a previous parametervalue within the specified amount of time, the data will come out of the cache. That way, no database call is required!!!

Check for more information the blog of Scott Guthrie or the official ASP .NET MVC 3 Releas Candidate page.