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...