Custom OutputCache Attribute in ASP.NET MVC

Custom OutputCache Attribute in ASP.NET MVC

In this article, I am going to discuss How To Create Custom OutputCache Attribute in ASP.NET MVC application. Please read our previous article where we discussed how to use VarByParam, Location, and Output Cache Profile options of Outputcache Attribute with examples. We are also going to work with the same example that we worked on within our previous two articles.

Using Cache Profiles with Child Action method:

In the following example, the GetEmployeeCount() method is decorated with two attributes i.e. ChildActionOnly and OutputCache. As you can see the OutputCache attribute uses the CacheProfile. If you remember in our previous article we create the cache profile in the web.config file.

[ChildActionOnly]
[OutputCache(CacheProfile = "1MinuteCache")]
public string GetEmployeeCount()
{
    return "Employee Count = " + db.Employees.Count().ToString()
        + "@ " + DateTime.Now.ToString();
}

When you use the Cache Profiles with child action methods, then you will get an error saying – Duration must be a positive number. There are many different ways to make the cache profiles work with child action methods. Here, in this article, I am going to create a Custom Outputcache Attribute to make the cache profiles work with the child action methods.

Create a custom OutputCache attribute that loads the cache settings from the cache profile in web.config.

Step1: Right-click on the project name in solution explorer, and add a folder with the name Common.
Step2: Right-click on the “Common” folder and add a class file with the name PartialCacheAttribute.cs
Step3: Copy and paste the following code. Notice that, I have named the custom OutputCache attribute as PartialCacheAttribute.

using System.Web.Configuration;
using System.Web.Mvc;

namespace CachinginMVC.Common
{
    public class PartialCacheAttribute : OutputCacheAttribute
    {
        public PartialCacheAttribute(string cacheProfileName)
        {
            OutputCacheSettingsSection cacheSettings = (OutputCacheSettingsSection)WebConfigurationManager.GetSection("system.web/caching/outputCacheSettings");
            OutputCacheProfile cacheProfile = cacheSettings.OutputCacheProfiles[cacheProfileName];
            Duration = cacheProfile.Duration;
            VaryByParam = cacheProfile.VaryByParam;
            VaryByCustom = cacheProfile.VaryByCustom;
        }
    }
}

Step4: Use PartialCacheAttribute on the child action method and pass the cache profile name that is created in the web.config file. Please note that PartialCacheAttribute is in CachinginMVC.Common namespace.

[ChildActionOnly]
[PartialCache("1MinuteCache")]
public string GetEmployeeCount()
{
    return "Employee Count = " + db.Employees.Count().ToString()
        + "@ " + DateTime.Now.ToString();
}

In the next article, I am going to discuss ValidateInput Attribute in the ASP.NET MVC application. In this article, I try to explain how to Customizing OutputCache Attribute in ASP.NET MVC application step by step with a simple example. I hope you understood how to create a custom output cache attribute in MVC.