Category >
ASP.NET MVC
|| Published on :
Monday, March 20, 2023 || Views:
1238
||
ASP.NET MVC Filters Authorization Filters Action Filters Result Filters Exception Filters
Filters are a fundamental concept in ASP.NET MVC that allow developers to implement cross-cutting concerns in their applications. They provide a way to add functionality to controllers and actions without having to modify the code directly. In this article, we will explore the four types of filters in ASP.NET MVC and how to use them in your application.
Filters in ASP.NET MVC are used to execute code before or after an action method is executed. Filters can be used to implement cross-cutting concerns such as authentication, caching, logging, exception handling, and more. In this article, we will explore different types of filters in ASP.NET MVC and how to use them in your application.
Types of Filters
There are four types of filters in ASP.NET MVC:
- Authorization Filters
- Action Filters
- Result Filters
- Exception Filters
Authorization Filters
Authorization Filters are used to restrict access to an action method based on certain conditions. These filters can be applied at the global, controller, or action level. Authorization filters can be further divided into the following categories:
-
AuthorizeAttribute - Restricts access to an action method to authenticated users.
-
AllowAnonymousAttribute - Allows anonymous access to an action method.
The following example shows how to implement an Authorization Filter in ASP.NET MVC:
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
{
filterContext.Result = new HttpUnauthorizedResult();
}
}
}
In the above example, we have created a custom Authorization filter that restricts access to an action method to authenticated users. The filter derives from the AuthorizeAttribute class and overrides the OnAuthorization method, which is executed before an action method is executed. If the user is not authenticated, the filter returns an HttpUnauthorizedResult.
Action Filters
Action Filters are used to execute code before or after an action method is executed. These filters can be applied at the global, controller, or action level. Action filters can be further divided into the following categories:
-
OnActionExecuting - Executed before an action method is executed.
-
OnActionExecuted - Executed after an action method is executed.
The following example shows how to implement an Action Filter in ASP.NET MVC:
public class CustomActionFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.HttpContext.Response.Write("Action is executing...");
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
filterContext.HttpContext.Response.Write("Action has executed...");
}
}
In the above example, we have created a custom Action filter that writes a message to the HTTP response before and after an action method is executed. The filter derives from the ActionFilterAttribute class and overrides the OnActionExecuting and OnActionExecuted methods.
Result Filters
Result Filters are used to modify or monitor the result returned by an action method. These filters can be applied at the global, controller, or action level. Result filters can be further divided into the following categories:
-
OnResultExecuting - Executed before the result returned by an action method is executed.
-
OnResultExecuted - Executed after the result returned by an action method is executed.
The following example shows how to implement a Result Filter in ASP.NET MVC:
public class CustomResultFilterAttribute : ResultFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
filterContext.HttpContext.Response.Write("Result is executing...");
}
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
filterContext.HttpContext.Response.Write("Result has executed...");
}
}
In the above example, we have created a custom Result filter that writes a message to the HTTP response before and after the result returned by an action method is executed. The filter derives from the ResultFilterAttribute