Does anyone know if it’s possible to cancel output caching in code? What I mean is if I place output caching on a child action as follows can I then based on a condition cancel that caching from inside the child action?
[ChildActionOnly]
[OutputCache(Duration = 36000, VaryByParam="tagslug")]
public virtual ActionResult MostViewed(string tagslug, int count)
{
// Make an API call here. If not data returned do not cache the ChildAction as specified above
}
Skimming the framework source it looks like the only logic is don’t-cache-on-exception:
I can’t see a brilliant way to solve this: I think you’ll have to make your own custom
OutputCachingAttributebased on the original source from the ASP.NET MVC source from CodePlex, and then either add an extra check to that line for the output returned e.g.or similar, or find a way to pass that code a flag to this from your controller. The existing code uses an object to store a value on the session; you could copy this, e.g.
_childActionFilterFinishCallbackKeye.g._noCacheResultKeyadd a public static method to the attribute that you can call e.g.
ClearChildActionFilterFinishCallbackto remove this from.Items[]as well as the callbackextend the above test to check this too e.g.
from your controller call
MyOutputCacheAttribute.FlagNoCache(Context);as necessary.It may also be possible to throw an exception from your code and then catch it in a different
IExceptionFilterso that it doesn’t get passed up beyond theOutputCacheAttributebut I don’t know how sorry.