In this recipe, you will learn how to create a resource filter and when to use it.
After authorization filters, resource filters are the first filters executed on the filter pipeline. This filter is executed after the ActionFilter
if no exception occurs. It's also the last filter eventually executed, leaving the filter pipeline.
A common resource
filter implementation is cache managing. The OutputCache
attribute in MVC is an example of a resource filter.
To create our own resource filter, we have to create a class that derives from IResourceFilter
or IAsyncResourceFilter
, depending on whether we want to create a synchronous or asynchronous filter.
- First, let's create an empty synchronous resource filter:
public class MyResourceFilter : IResourceFilter { public void OnResourceExecuted(ResourceExecutedContext context) { // code executed before the filter executes } public...