Click here to Skip to main content
12,830,304 members (42,504 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


6 bookmarked
Posted 4 Jul 2014

Cheat Sheet for Enabling Output Caching in ASP.NET MVC

, 4 Jul 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Cheat sheet for enabling output caching in ASP.NET MVC

What is Output Caching

ASP.NET output caching feature allows increasing web application performance by returning content that was built some time ago instead of rebuilding it on every request. Returning content from cache only takes a few milliseconds as opposed to executing a full request cycle that could take much longer.

The content is being cached on the ASP.NET level and the request would not reach the application code for the content that is still in cache. ASP.NET output caching can be used in both WebForms and MVC applications and using it in MVC has become even easier than before.

When to Use Output Caching

Output caching is useful when content returned by a controller action method does not change frequently, requires more than few CPU cycles or database access and does not require a lot of memory to store. For example, I would not recommend to use output caching for a large binary object like an image or a file. Also, there is no point to cache a short string that only takes a few milliseconds to build. The best use case would be an average size content that requires some calculation or database access to produce but does not change on every request.

How to Enable Output Caching

The easiest way to enable output caching in MVC is using an OutputCache attribute on the controller or controller action method. Applying output caching on a controller action method is recommended as it gives much better granularity and control over output caching. The best way to control output caching behaviour is via caching profiles that allow defining all parameters in a web.config file and override them for each environment where the web application is deployed (i.e., Dev, QA, Stage, Live, etc.)


ASP.NET MVC has some limitations for output caching, for example: caching profile is not supported for a partial/child controller method therefore caching parameters including Duration and VaryByParam must be set in the code.

Implementation and Code Review Check Points

  1. Apply to individual action methods
  2. Use Caching profiles
  3. Partial/child action methods should not be cached for too long
  4. Disable output caching in Dev/QA/Stage environment.
  5. Do not forget to set Duration and VaryByParam values in web.config


<script class="brush: xml" type="syntaxhighlighter"><system.web> <caching> <outputcachesettings> <outputcacheprofiles> <add duration="60" name="cacheProfile" varybyparam="*"> </add></outputcacheprofiles> </outputcachesettings> </caching></system.web></script>
<script class="brush: csharp" type="syntaxhighlighter">
[OutputCache(CacheProfile = "cacheProfile")]
public ActionResult Test()
{ ViewData["result"] = "This text was rendered at " + DateTime.Now; return View(); }


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Alexander Turlov
Canada Canada
Alexander Turlov has been working in IT industry since 1987. His programming experience includes such languages as FORTRAN, Pascal, and Basic, C, C++ and C#. He's been working for different industries like science, manufacturing, retail, utilities, finance, insurance, health care, education and so on. His area of interests is rich web applications development with .NET, C#, ASP.NET/MVC and JavaScript. He is working in software development doing architecture, design and development on .NET platform and using Microsoft Visual Studio, IIS and SQL Server as his primary tools, and a lot of other commercial and open source frameworks and tools. He holds a M.Sc. degree in physics and an MCSD.NET certification.

View my profile on LinkedIn

View my blog

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170326.1 | Last Updated 5 Jul 2014
Article Copyright 2014 by Alexander Turlov
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid