In this recipe, you will learn how to create and use a result filter.
A result filter injects treatment before or after the result is executed. It could check and modify the result generated by action methods.
Some common result filter implementations could be adding or checking headers, and many others.
To create our own result filter, we have to create a class that derives from any of the following base classes or interfaces:
IResultFilter
orIAsyncResultFilter
, depending on whether we want to create a synchronous or an asynchronous filterResultFilterAttribute
As with IActionResult
/IAsyncActionResult
interfaces, IResultFilter
and IResultActionFilter
inherit from IFilterMetadata
. All the classes implemented in those interfaces can't use an attribute above a controller or action method. A class should implement IResultFilter
, or IResultActionFilter
and TypeFilter
, or ServiceFilter...