A Summary of ASP.NET MVC Filter Types

Datetime:2016-08-23 04:39:25          Topic: ASP.NET MVC           Share

Action Filters are attributes that can be applied either on a controller action method or on a controller. When applied at the controller level, they are applicable for all action methods within that controller.

The MVC pattern can provide one-to-one relationships between a controller action method and each user interaction. So Action Filters allow us to add pre- and post-processing logic to an action method. This article explores the basic ideas about ASP.NET MVC filters.

Types of Filters

The ASP.NET MVC framework provides five types of filters, one filter type newly added in MVC 5.

  • Authentication filters (New in ASP.NET MVC5)

  • Authorization filters

  • Action filters

  • Result filters

  • Exception filter

Authentication Filter

Authentication filter allow you to specify authentication logic per-action, per-controller, or globally for all controllers. The IAuthenticationFilter interface is used to create a Custom Authentication filter.

Example – Authenticate Login User

public interface IAuthenticationFilter

{

  void OnAuthentication(AuthenticationContext filterContext);

  void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext);

}

Authorization Filters

Authorization filters are taking care of authorizing the current user. Authorization filters enforce your authorization policy, ensuring that action methods can be invoked only by approved users. Authorization filters implement the IAuthorizationFilter interface.

Example – Authorize Filter

public interface IAuthorizationFilter

{

   void OnAuthorization(AuthorizationContext filterContext);

}

Action filters

Action filter is responsible for performing additional logic before and after the action execution. The IActionFilter interface is used to create an Action Filter.

Example – Modification in the Data Sent By View

public interface IActionFilter

{

    void OnActionExecuting(ActionExecutingContext filterContext);

    void OnActionExecuted(ActionExecutedContext filterContext);

}

An action filter is implemented as an attribute class that inherits from ActionFilterAttribute we need to override both the methods.

Result Filters

Result filters are similar to action filters, the difference is they run after the action method has executed, but before the result returned from the action method has been executed. The BEFORE method is called OnResultExecuting and the AFTER method is called OnResultExecuted.

Example – Modification in view result

public interface IResultFilter

{

   void OnResultExecuted(ResultExecutedContext filterContext);

   void OnResultExecuting(ResultExecutingContext filterContext);

}

Exception Filters

The exception filters handles errors raised by controller or action or result of execution of action. The exception filters implements the interface IExceptionFilter.

Example – Error Logging

public interface IExceptionFilter

{

   void OnException(ExceptionContext filterContext);

}

Filter Interfaces

Authentication Filter

IAuthenticationFilter

Authorization Filter

IAuthorizationFilter

Action Filter

IActionFilter

Result Filter

IResultFilter

Exception Filter

IExceptionFilter

FiltersExecution Order

As this was just touchup for ASP.NET MVC filters, I have a plan to share my code base in the next article.





About List