2

my aim is to have a global NoCacheActionFilter attribute (registered in the Global.asax.cs ) which disables caching by default.

Then I'd like to add more fine-grained control in that on the needed Actions of my controllers I'd like to add a caching attribute which should override the one defined in the global NoCacheActionFilter.

My Approach

in the NoCacheActionFilter

public override void OnActionExcecuting(ActionExecutingContext filterContext)
{
    //disable caching
    HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;
    cache.SetCacheability(HttpCacheability.NoCache);
    ...
}

and in the caching attribute ...

public override void OnActionExecuted(ActionExecutedContext filterContext)
{
    //add caching
    ...
    cache.SetCacheability(HttpCacheability.Public);
    ...
}

Note the difference between OnActionExecuting and OnActionExecuted, they should be invoked one after the other, so the order should be granted.

However, apparently this does not work. Any suggestions?? I have the feeling to miss some trivial thing...

Juri
  • 32,424
  • 20
  • 102
  • 136
  • The only way I managed to get something similar working, is by first invoking the private method `HttpCachePolicy.Reset()` [by using reflection](https://stackoverflow.com/a/28445308) – StuartLC Aug 19 '19 at 11:03

2 Answers2

0

Just a guess. After changing the Cache object of the filterContext do you call the method of the base filter class?

    ...
    base.OnActionExecuting(filterContext);
}

and

    ...
    base.OnActionExecuted(filterContext);
}
splattne
  • 102,760
  • 52
  • 202
  • 249
  • Nope...unfortunately adding it didn't change the behavior...still the global cache isn't being overridden. Is there maybe a constraint that global action filters always win over those declared specifically on controller actions?? (Although that would be really awkward) – Juri Jun 08 '12 at 12:30
0

It could be that the order the filters are running in are not what you expect.

Check out this blog post which explains the order they will run in: http://blog.rajsoftware.com/post/2011/05/14/MVC3-Filter-Ordering.aspx

nootn
  • 851
  • 1
  • 12
  • 16