Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9178551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:34:43+00:00 2026-06-17T17:34:43+00:00

OutputCache attribute on action that returns Json is not working – when I hit

  • 0

OutputCache attribute on action that returns Json is not working – when I hit the action URL in browser many times, each time I got the breakpoint activated in VS2012 (it looks like the OutputCache attribute is ignored). Here is my code:

public class ApiController : GenericControllerBase
{

    [OutputCache(Duration = 300, VaryByParam = "type;showEmpty;sort;platform")]
    public JsonResult GetCategories(string type, bool? showEmpty, string sort, string platform)
    {
        ///... creating categoryResults object
        return Json(new ApiResult() { Result = categoryResults }, JsonRequestBehavior.AllowGet);
    }

}   

GenericControllerBase inherits from Controller. In other Controllers inheriting from GenericControllerBase OutputCache works as expected, but they return View() instead of Json.
As an experiment I added VaryByCustom parameter and checked that GetVaryByCustomString method in global.asax file was not hit either, so the caching functionality is completely ignored.
I use MVC3 and autofac for service injection (but other controllers using autofac injection work properly with OutputCache).

What could be the problem? What could block OutputCache functionality? Is it possible that it has something to do with caching the whole response? All other actions using OutputCache in my projects are partial actions embeded with @Html.Action(…) in views.

What is the best way to cache whole Json response on GET action in MVC3?

Update

After some tests it turned out that OutputCache is ignored for actions returning complete pages (not only json). Cache works only for childactions in my project. What could be the cause?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:34:44+00:00Added an answer on June 17, 2026 at 5:34 pm

    Eventually I used a workaround for MVC3 ignoring OutputCache attribute. I used custom action filter for caching:

    public class ManualActionCacheAttribute : ActionFilterAttribute
    {
        public ManualActionCacheAttribute()
        {
        }
    
        public int Duration { get; set; }
    
        private string cachedKey = null;
    
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string key = filterContext.HttpContext.Request.Url.PathAndQuery;
            this.cachedKey = "CustomResultCache-" + key;
            if (filterContext.HttpContext.Cache[this.cachedKey] != null)
            {
                filterContext.Result = (ActionResult)filterContext.HttpContext.Cache[this.cachedKey];
            }
            else
            {
                base.OnActionExecuting(filterContext);
            }
        }
    
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            filterContext.HttpContext.Cache.Add(this.cachedKey, filterContext.Result, null, DateTime.Now.AddSeconds(Duration), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, null);
            base.OnActionExecuted(filterContext);
        }
    }
    

    Above attribute caches the request based on exact URL path and query for given time, and it works as expected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems outputcache filter does not apply when a controller action returns a RedirectResult
I'm trying to outputcache a action method in asp.net mvc. The method returns json
If the controller action has the OutputCache attribute specified on an action, is there
I'm using the OutputCache attribute to cache my action's html output at the server-side.
This is not working for some reason: [OutputCache(Duration = 600, VaryByParam = id)] public
I have Outputcache attribute above one of my controller action in an ASP.NET MVC
I have a controller ActionResult that returns JSON that I would like to cache.
In ASP.NET MVC2 I use OutputCache and the VaryByParam attribute. I got it working
Image i have a view that is cached with the OutputCache attribute but i
Currently ASP.NET MVC's OutputCache attribute has a huge downfall. If you want to cache

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.