What is Filters ?
Filters are mechanisms or components used in various software architectures and frameworks to intercept and process data or events as they flow through a system. Filters allow developers to perform actions such as validation, transformation, logging, security checks, and more, without directly modifying the core logic of the application. And the role of filters Mvc is given in the next paragraph.
MVC (Model-View-Controller) architecture, filters are used to customize the request processing pipeline and implement specific behavior. The role of filters in MVC is similar to that of a security guard or gatekeeper who stands at the entrance of a building.
Type of Filters in MVC
· Action Filters
· Result Filters
· Authentication Filters
· Authoriztion Filters
· Exception Filters
Action Filters
Action filters are used to implement the logic that get executed before or after a controller action executes. The IActionFilter interface is used to create an Action Filter. That interface provides two methods: OnActionExecuting and OnActionExecuted. These methods will be executed before or after an action is executed, respectively.
Example:
Result Filters
Result filters contain logic for executing before or after generating the result for an action. The IResultFilter interface is used to create a Result Filter.
Result filters are performed through the IResultFilter interface. That interface provides two methods: OnResultExecuting and OnResultExecuted. These methods will be executed before or after generating the result for an action.
Example:
Authentication Filters
Authentication filters are used in network security to identify and verify users to prevent unauthorized access. Authentication filters basically refer to protocols, algorithms, or methodologies that authenticate user identities. These filters typically use username-password combinations, biometric data, smart cards, or token-based systems. Create a class and replace the below code to create the custom authentication. ResloveActionFilterAttribute and IAuthenticationFilter form the “using System.Web.MVC.Filters;” namespace.
Example:
Authoriztion Filters
Authorization happens after authentication. Once you have verified your identity through authentication, the authorization process decides what resources and services you can access. Authorization basically verifies what level of permissions and privileges you can access with. The attributes taken below are used in authorizations filters.
Example:
Exception Filters
The HandleErrorAttribute class is an example of ExceptionFilters. These implement the IExceptionFilter interface and they execute if there are any unhandled exceptions thrown during the execution pipeline. These filters can be used as an exception filter to handle errors raised by either your controller actions or controller action results.
Example:
Take the first step towards data-led growth by partnering with MSA Infotech. Whether you seek tailored solutions or expert consultation, we are here to help you harness the power of data for your business. Contact us today and let’s embark on this transformative data adventure together. Get a free consultation today!
We utilize data to transform ourselves, our clients, and the world.
Partnership with leading data platforms and certified talents