The OutputCache attribute is commonly given as an example of a result filter in ASP.NET MVC. This MSDN page is one place. As such it wraps execution of the ActionResult object. But isn't that object executed at the end of the action method? I'm confused as to how it allows a cached response to be used and prevents the action itself from executing if it only wraps execution of the ActionResult at the end of that method. I know that caching works, so there is clearly some obvious piece that I'm missing.
The OutputCacheAttribute
inherits from ActionFilterAttribute
, which in turn implements IActionFilter
, and IResultFilter
. Thus the OutputCacheAttribute
is both an action filter and a result filter.
When you think about it, this makes perfect sense. The logic behind a cache goes like this:
So part 1 is handled by the implementation of IActionFilter
, if that doesn't immediately return a result, we continue with the action and the implementation of IResultFilter
handles adding that result to the cache for future calls.
Thanks to ASP.NET being open source, this can be confirmed in code. Check out OutputCacheAttribute.cs
on codeplex.
Line 222 is where the cahce is checked during OnActionExecuting
(part of IActionFilter
)
Line 237 - 249 the OnActionExecuting
method sets up a callback that gets invoked during OnResultExecuted
(part of IResultFilter
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With