Asp.Net MVC Exploring Controllers in depth Part 5 – Bestdotnettraining

Asp.Net MVC Exploring Controllers in-depth Part 5 – Bestdotnettraining

Filter Types

    Description: Asp.Net MVC Exploring Controllers in-depth      Part 5

ASP.NET MVC supports the following types of filters:

1.        Authorization Filter:

  • Class Implementing IAuthorizationFilter
  • Eg: AuthorizeAttribute and RequireHttpsAttribute
  • Can override the OnAuthorization method

2.        Action Filter:

  • Class Implementing IActionFilter
  • Eg: ActionFilterAttribute – Used for writing Custom Attributes
  • Can override OnActionExecuting and OnActionExecuted

3.        Result Filter:

  • Class Implementing IResultFilter
  • Eg:
  • ActionFilterAttribute implements both IResultFilter and IActionFilter.
  • Can override OnResultExecuting and OnResultExecuted

4.        Exception Filter:

  • Class Implementing IExceptionFilter
  • Eg: HandleErrorAttribute

Note: Controller class Implements all of the above and we can override in it all the methods

mentioned.

Building Custom Action Filters

An action filter is implemented as an attribute class that inherits from ActionFilterAttribute.

The base ActionFilterAttribute class has the following methods that you can override:

  • OnActionExecuting – This method is called before a controller action method is
  • OnActionExecuted – This method is called after a controller action method is
  • OnResultExecuting – This method is called before the ActionResult instance that is returned by your action is
  • OnResultExecuted – This method is called after the ActionResult instance that is returned by your action is

The following example shows a simple action filter that logs trace messages

Applying Custom Action filter to a controller or Action method:

Global Registering Action filters

 

visit for complete MVC online training 

 

Please follow and like us:
0

Leave a Reply

Your email address will not be published. Required fields are marked *