In this article, we will learn how to use OutputCache Action Filter in ASP.NET MVC Applications. In previous ASP.NET MVC tutorials of this series, we saw
- Creating First Application In ASP.NET MVC
- Pass Parameter Or Query String In Action Method In ASP.NET MVC
- Passing Data from Controller To View In ASP.NET MVC
- Strongly Typed View Vs Dynamically Typed View In ASP.NET MVC
- Working With Built-In HTML Helper Classes In ASP.NET MVC
- Inline and Custom HTML Helpers In ASP.NET MVC
- CRUD Operation In ASP.NET MVC Using AJAX And Bootstrap
- HandleError Action Filter In ASP.NET MVC
- Deploy ASP.NET MVC Application To Windows Azure
One of the best ways to improve the performance of an ASP.NET MVC Application is by caching. With the help of caching, we can reduce hosting and the database Server round trips. We can apply Outputcache Action Filter either on Action Method or on the controller. OutputCache attribute has several properties like CacheProfile, Duration, Location, VaryByParam, VaryByHeader, NoStore etc.

Let’s create a simple example, so that we can understand OutputCache attribute better.

Let’s create a simple example, so that we can understand OutputCache attribute better.
Let’s begin
Create a new ASP.NET MVC Application.
Select Empty MVC Application and click OK.

For this example, I have created a database (named as DemoDB) with a table (Employee) having schema, as shown below. (I will use Entity Framework with Database First approach. In this example, you can use any with which you feel comfortable).
Create a new ASP.NET MVC Application.
Select Empty MVC Application and click OK.

For this example, I have created a database (named as DemoDB) with a table (Employee) having schema, as shown below. (I will use Entity Framework with Database First approach. In this example, you can use any with which you feel comfortable).
- CREATE TABLE dbo.Employee
- (
- ID INT IDENTITY NOT NULL,
- Name NVARCHAR (100) NULL,
- City NVARCHAR (100) NULL,
- CONSTRAINT PK_Employee PRIMARY KEY (ID)
- )
- GO
Right click on Models and then click on Add new item. Select ADO.NET Entity Data Model. Give it a meaningful name and click Add.

Select EF Designer from the database and click next.

Create New Connection and select version of Entity framework, which you want to use.

Select EF Designer from the database and click next.

Create New Connection and select version of Entity framework, which you want to use.

Select the database objects which you want to add in your model and click finish.
Afterwards, you will see Employee Entity in Designer.
Now, right click on the controller and add Empty controller.
Bind the employee data to the view.
- public ActionResult Index()
- {
- DemoDBEntities db = new DemoDBEntities();
- var Employees = db.Employees.ToList();
- return View(Employees);
- }



Kirill BorunovPosted Apr 20, 2021, 5:56 PM
Why do we need VaryByParam = "none" when using Location = System.Web.UI.OutputCacheLocation.Client?
Mike SPosted Jan 25, 2019, 9:02 AM
Anoop, how do (can I) use VaryByCustom to cache content by language. My language code is saved in a cookie called lang. lang can be en, fr, de. How do i cache by lang value? Thanks.
Chetan KhulePosted Jul 29, 2018, 1:16 AM
Keep it up.. Easy to understand
Sam BPosted Oct 12, 2017, 11:20 AM
Where are you putting this setting? In Web.Config? Under which section? These articles never have all the details... <caching> <outputCacheSettings> <outputCacheProfiles> <add name="CacheFor20Seconds" duration="20" varyByParam="none" location="Server"/> </outputCacheProfiles> </outputCacheSettings> </caching>
hardik gheewalaPosted Jul 23, 2017, 11:10 AM
Gif explanation is good concept!! nice article
mohit SPosted Jun 14, 2017, 2:49 AM
Very good article explain in simple language keep it up..