Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Filters in ASP.NET MVC

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 602
    Comment on it

    Filters are used where we want to execute code before Action Call and after Action call. Filters are custom classes that provide pre-action and post-action behaviour to controller actions.

     

    Types of Filters:

     

    ASP.NET MVC provides following types of filters:

     

    1. Authorization filters - These implement IAuthorizationFilter. Authorization filter is used to make filter decision. It performs authorization before execute action method and validating properties of request.    Authorization filter is the first filter to execute. Authorize filter is an example of an Authorization filter.
    The IAuthenticationFilter interface is used to create CustomAuthentication filter. The definition of this interface is given below:

    public interface IAuthorizationFilter
    {
     void OnAuthorization(AuthorizationContext filterContext);
    }

     

    2. Action filters - These implement IActionFilter. Action filters are used before and after an action executes to implement the logic that are executed before and after action executes. IActionFilter interface is used to create an Action Filter. It provides two mehods OnActionExecuting and OnActionExecuted. OnActionExecuting will be executed before the action call and OnActionExecuted will be executed after the action call.
    The definition of this interface is given below:

    public interface IActionFilter
    {
     void OnActionExecuting(ActionExecutingContext filterContext);
     void OnActionExecuted(ActionExecutedContext filterContext);
    } 
    

     

    ASP.NET MVC provides following action filters:

    Output Cache: This filter is used to cache the output of a controller action for given amount of time.
    Handle Error: This filter handles error raised when controller action executes.
    Authorize: This filter is used to restrict access to particular user or role.

     

    3. Result filters - These implement IResultFilter. Result filters are used to perform additional processing in the view result. Result filters are executed before or after generating the result for an action. It called after the the Action filters. The ActionResult type can be ViewResult, PartialViewResult, RedirectToRouteResult, RedirectResult, ContentResult, JsonResult, FileResult and EmptyResult.
    The IResultFilter interface is used to create an Result Filter.It provides two methods OnResultExecuting and OnResultExecuted. OnResultExecuting will be executed before generating the result for an action and OnResultExecuted will be called after generating the result for an action.
    The definition of this interface is given below:

    public interface IResultFilter
    {
     void OnResultExecuted(ResultExecutedContext filterContext);
     void OnResultExecuting(ResultExecutingContext filterContext);
    } 

     

    4. Exception filters - These implement IExceptionFilter. Exception filters are used to execute the code, if there is unhandled exception is thrown in action method. Exception filter are used for logging or displaying error page.
    IExceptionFilter interface is used to create an Exception Filter. It provides OnException method. OnException method will be executed when exception occurs during the actions execution.
    The definition of this interface is given below:

    public interface IExceptionFilter
    {
     void OnException(ExceptionContext filterContext);
    }

     

    Filters Sequence:

    Filters run in the following sequence:
    1. Authorization filters
    2. Action filters
    3. Response filters
    4. Exception filters

     

    Each type of filter is executed in a particular order. If we want to change the filter order then we can set a filter's order property.

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: