Filters in MVC
What are filters in MVC?
1949
25-Dec-2019
Updated on 25-Dec-2019
Rahul Roi
27-Oct-2020The types of Filters in MVC:-
Into the ASP.NET MVC framework provide the following action filters in MVC:
Shikhar Arora
25-Dec-2019In MVC, controllers define action methods and these action methods normally have a one-to-one relationship with UI controls such as clicking a button or a link, etc. For example, in one of our previous examples, the UserController class consists of methods UserAdd, UserDelete, etc.
But many times we would like to perform some action before or after an exceptional operation. To accomplish this functionality, ASP.NET MVC provides a feature to add pre and post-action behaviors on the controller's action methods.
Types of Filters
ASP.NET MVC framework can support the following action filters,
Action Filters
Action filters are used to apply the logic that gets executed before and after a controller action executes. We will look at Action Filters in elaboration in this chapter.
Authorization Filters
Authorization filters are used to apply authentication and authorization for controller actions.
Result Filters
Result filters comprise of logic that is executed before and after a view result is executed. For example, you might want to reform a view result right before the view is rendered to the browser.
Exception Filters
Exception filters are the last type of filter to run. You can use an exception filter to hold errors raised by either your controller actions or controller action results. You can also avail exception filters to log errors.
Action filters are one of the most commonly used filters to perform additional data processing, or manipulating the return values or canceling the execution of an action or modifying the view structure at run time.