Types of filters in MVC
Dec 14, 2020 08:26 0 Comments ASP.net MVC PARTH

                     Types of filters in MVC

 

We have five types of filters in the ASP.NET MVC framework –

  • Authentication filters (New in ASP.NET MVC5)
  • Authorization filters
  • Action filters
  • Result filters
  • Exception filter

Authentication Filter

Authentication filter is used 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

public interface IAuthenticationFilter

{

  void OnAuthentication(AuthenticationContext filterContext);

  void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext);

}

 

Authorization Filters

Authorization filters are used to take 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

public interface IAuthorizationFilter

{

   void OnAuthorization(AuthorizationContext filterContext);

}

Action filters

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

Example

public interface IActionFilter

{

    void OnActionExecuting(ActionExecutingContext filterContext);

    void OnActionExecuted(ActionExecutedContext filterContext);

}

Result Filters

Result filters are similar to action filters, the difference is that 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

public interface IResultFilter

{

   void OnResultExecuted(ResultExecutedContext filterContext);

   void OnResultExecuting(ResultExecutingContext filterContext);

}

 

Exception Filters

The exception filters are used to handle errors raised by controller or action or result of execution of action. The exception filters implement the interface IExceptionFilter.

Example – Error Logging

public interface IExceptionFilter

{

   void OnException(ExceptionContext filterContext);

}

Prev Next
About the Author
Topic Replies (0)
Leave a Reply
Guest User

You might also like

Not sure what course is right for you?

Choose the right course for you.
Get the help of our experts and find a course that best suits your needs.


Let`s Connect