|
Answer» The five types of Filters in ASP.NET MVC and order of their execution are as follows: - Authentication Filters: It executes before the execution of any other filter or action method. Authentication filter will make sure about whether you are a valid or INVALID user. These filters will implement the interface IAuthenticationFilter.
- Authorization Filters: It is responsible for checking User Access and is useful in implementing the authentication as well as authorization for controller actions. These authorization filters will implement the interface IAuthorizationFilterinterface. Examples of an Authorization filter are:
- AuthorizeAttribute: It restricts access by optionally authorization and authentication. - RequireHttpsAttribute: It forces HTTP(HyperText Transfer Protocol) requests that are unsecured to be resent over HTTPS (HyperText Transfer Protocol Secure). - Action Filters: It is an attribute that can be applied to a controller action or a complete controller. It will be called before the execution of action begins and after the execution of action COMES to end. These action filters will implement the interface IActionFilter in the framework, which has OnActionExecuting and OnActionExecuted methods. OnActionExecuting method executes before the Action and provides an opportunity for Action call cancelling. Since action filters are executed before and after a controller action executes, they can also be used for modifying the view data that is returned by a controller action.
- Result Filters: It contains a logic that is to be executed before and after the execution of a view result. That means if you wish to modify a view result exactly before the rendering of view to the browser. These result filters are implemented from the IResultFilter interface in the framework, which has two methods namely OnResultExecuting and OnResultExecuted. An example of a result Filter is OutputCacheAttribute class which will provide OUTPUT caching.
- ExceptionFilters: It can be used for handling the ERRORS that are raised by the controller actions or by the results of controller actions. The exception filters will implement the IExceptionFilter interface in the framework. It is very helpful during the execution pipeline where any unhandled EXCEPTIONS might be thrown. An example for ExceptionFilter is HandleErrorAttribute class which will specify the way for handling an exception thrown by an action method.
It is possible to override the methods in your controller class if you wish to do so.
|