2

I have a ASP.Net MVC5 application. I disabled caching through out the application by applying global filter as follows:

public class CachingFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache); // HTTP 1.1.
        filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
        filterContext.HttpContext.Response.Cache.AppendCacheExtension("no-store, must-revalidate");

        filterContext.HttpContext.Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.0.
        filterContext.HttpContext.Response.AppendHeader("Expires", "0"); // HTTP 1.0.
    }
}

The filter above disables caching brilliantly. But now I have an action to populate some statistics as a PartialView. For test purposes I wanted to enable caching for 20 seconds, by applying OutputCacheAttribute as follows:

    [AcceptVerbs(HttpVerbs.Get)]
    [OutputCache(Location = OutputCacheLocation.Client, Duration = 20, VaryByParam = "*")]
    public PartialViewResult Statistics()
    {
        var stats = GetStatistics();
        return PartialView("~/Views/Shared/_Statistics.cshtml", stats);
    }

No matter what I did, If CachingFilter is enabled in application global, Statistics() method is always called even though 20 second period isn't elapsed. If I disable CachingFilter from global, Statistics() method is cached properly.

I thought/read that applying cache filter to action is the final verdict for caching. How to bypass global caching properties in action level without adding action/controller name in if clauses in global cache filter?

1 Answer 1

1

You can create your own attribute to exclude the global filter on certain attributes, for example, create a stub attribute:

public class ExcludeCacheFilterAttribute : Attribute
{
}

Now in CachingFilter check for this attribute before running your code:

public class CachingFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext.ActionDescriptor.GetCustomAttributes(typeof(ExcludeCacheFilterAttribute), false).Any())
        {
            return;
        }            

        //Carry on with the rest of your usual caching code here
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi @DavidG, thanks for your effort but that doesn't helped. I tried to apply ExcludeCacheFilterAttribute to Controller too but I still cannot cache my Action.
So did you try to debug it? Does it exit the OnActionExecuting method before sending the cache detail correctly?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.