We write custom action filters for various reasons. We may have a custom action filter for logging, or for saving data to database before any action execution. We could also have one for fetching data from the database and setting it as the global values of the application.
To create a custom action filter, we need to perform the following tasks:
Create a class
Inherit it from ActionFilterAttribute class
Override at least one of the following methods:
OnActionExecuting – This method is called before a controller action is executed.
OnActionExecuted – This method is called after a controller action is executed.
OnResultExecuting – This method is called before a controller action result is executed.
OnResultExecuted – This method is called after a controller action result is executed.
The filter can be created as shown in the listing below:
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0