Category >
ASP.NET MVC
|| Published on :
Wednesday, March 22, 2023 || Views:
1230
||
ASP.NET MVC Action Filters Authorization Filters Result Filters Exception Filters C# Examples
This article provides an in-depth understanding of Action Filters in ASP.NET MVC and how they can be used to handle cross-cutting concerns. It explains the four types of Action Filters in detail with practical examples. This guide is helpful for developers who want to learn how to use Action Filters to enhance the functionality of their ASP.NET MVC applications.
ActionFilter Attributes in ASP.NET MVC
ASP.NET MVC provides a powerful and flexible way to handle cross-cutting concerns using Action Filters. Action Filters are attributes that can be applied to controller actions or the controller itself to modify the way the action is executed or to add behavior to the action. They are a key component in the ASP.NET MVC framework and can be used for a wide range of purposes, such as logging, caching, authorization, and validation.
In this article, we will explore the basics of Action Filters in ASP.NET MVC and provide some practical examples of how they can be used.
What are Action Filters?
Action Filters are attributes that can be applied to controller actions or the controller itself to modify the way the action is executed or to add behavior to the action. They are used to handle cross-cutting concerns in an ASP.NET MVC application, such as logging, caching, authorization, and validation.
Action Filters are executed before and after an action is executed. There are four types of Action Filters:
- Authorization Filters
- Action Filters
- Result Filters
- Exception Filters
Authorization Filters
Authorization Filters are used to restrict access to controller actions. They are executed before the action is executed and are used to check if the user is authorized to access the action. Authorization Filters can be used to restrict access to specific users or roles, or to apply custom authorization logic.
The [Authorize] attribute is an example of an Authorization Filter. It is used to restrict access to a controller action to users who are authenticated.
Example:
[Authorize]
public ActionResult MyAction()
{
// Action code here
}
This action can only be accessed by authenticated users.
Action Filters
Action Filters are used to modify the way an action is executed. They can be used to perform tasks before and after an action is executed, such as logging, caching, and validation.
Action Filters are executed in the following order:
- OnActionExecuting: This method is executed before the action is executed.
- OnActionExecuted: This method is executed after the action is executed.
Example:
public class LogActionFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
// Log action execution start
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
// Log action execution end
}
}
[LogActionFilter]
public ActionResult MyAction()
{
// Action code here
}
This action will be logged before and after it is executed.
Result Filters
Result Filters are used to modify the result of an action. They are executed before and after the action result is executed, and can be used to modify the action result or to perform tasks related to the result.
Result Filters are executed in the following order:
- OnResultExecuting: This method is executed before the action result is executed.
- OnResultExecuted: This method is executed after the action result is executed.
Example:
public class LogResultFilter : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
// Log result execution start
}
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
// Log result execution end
}
}
[LogResultFilter]
public ActionResult MyAction()
{
// Action code here
return View();
}
his action will be logged before and after the result is executed.
Exception Filters
Exception Filters are used to handle exceptions that occur during the execution of an action. They are executed when an exception is thrown from an action, and can be used to handle the exception, log the exception, or to perform other tasks related to the exception.