Filters are a mechanism for applying cross-cutting
concerns to controller actions. Filters allow you to perform tasks such
as authentication, logging, caching, and exception handling in a modular
and reusable way. They are a key part of MVC's extensibility and help
keep your code clean and maintainable by separating concerns.
Filters in ASP.NET MVC are used to inject logic that runs before or after an action method is executed.
Authorization Filters: Authorization filters are used to enforce authentication and authorization rules.
They
run before the action method is executed and can be used to verify
whether the user is authenticated and authorized to perform the
requested action.
Action Filters: Action filters run before and after an action method is executed. They can be used to modify the behavior of the action method or perform pre-processing or post-processing tasks.
Result Filters: Result filters run before and after the result of an action method is executed. They can be used to modify the behavior of the result or perform post-processing tasks.
Exception Filters: Exception filters are used to handle exceptions that occur during the execution of an action method. They run only when an unhandled exception occurs.