What are different types of Filters in ASP.NET MVC?

The ASP.NET MVC framework provides five types of filters.

  1. Authentication Filters – This filter is introduced with ASP.NET MVC5. The IAuthenticationFilter interface is used to create CustomAuthentication filter. The definition of this interface is given below-
    public interface IAuthenticationFilter {
    	void OnAuthentication(AuthenticationContext filterContext);
    	void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext);
    }

    You can create your CustomAuthentication filter attribute by implementing IAuthenticationFilter as shown below-

    public class CustomAuthenticationFilterAttribute: FilterAttribute,
    IAuthenticationFilter {
    	public void OnAuthentication(AuthenticationContext filterContext) {
    		filterContext.HttpContext.Response.Write("Authentication Filter<br/>");
    	}
    	//Runs after the OnAuthentication method
    	public void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext) {
    		//TODO: Additional tasks on the request
    	}
    }
  2. Authorization Filters – The ASP.NET MVC Authorize filter attribute implements the IAuthorizationFilter interface. The definition of this interface is given below-
    public interface IAuthorizationFilter {
    	void OnAuthorization(AuthorizationContext filterContext);
    }

    The AuthorizeAttribute class provides the following methods to override in the CustomAuthorize attribute class.

    public class AuthorizeAttribute: FilterAttribute,
    IAuthorizationFilter {
    	protected virtual bool AuthorizeCore(HttpContextBase httpContext);
    	protected virtual void HandleUnauthorizedRequest(AuthorizationContext filterContext);
    	public virtual void OnAuthorization(AuthorizationContext filterContext);
    	protected virtual HttpValidationStatus OnCacheAuthorization(HttpContextBase httpContext);
    }

    In this way you can make your CustomAuthorize filter attribute either by implementing IAuthorizationFilter interface or by inheriting and overriding above methods of AuthorizeAttribute class.

  3. Action Filters – Action filters are executed before or after an action is executed. The IActionFilter interface is used to create an Action Filter which provides two methods OnActionExecuting and OnActionExecuted which will be executed before or after an action is executed respectively.
    public interface IActionFilter {
    	void OnActionExecuting(ActionExecutingContext filterContext);
    	void OnActionExecuted(ActionExecutedContext filterContext);
    }
  4. Result Filters – Result filters are executed before or after generating the result for an action. The Action Result type can be ViewResult, PartialViewResult, RedirectToRouteResult, RedirectResult, ContentResult, JsonResult, FileResult and EmptyResult which derives from the ActionResult class. Result filters are called after the Action filters. The IResultFilter interface is used to create a Result Filter which provides two methods OnResultExecuting and OnResultExecuted which will be executed before or after generating the result for an action respectively.
    public interface IResultFilter {
    	void OnResultExecuted(ResultExecutedContext filterContext);
    	void OnResultExecuting(ResultExecutingContext filterContext);
    }
  5. Exception Filters – Exception filters are executed when exception occurs during the actions execution or filters execution. The IExceptionFilter interface is used to create an Exception Filter which provides OnException method which will be executed when exception occurs during the actions execution or filters execution.
    public interface IExceptionFilter
    {
          void OnException(ExceptionContext filterContext);
    }

    The HandleErrorAttribute class is one example of an exception filter which implements IExceptionFilter. When HandleError filter receives the exception it returns an Error view located in the Views/Shared folder of your ASP.NET MVC application.

Tagged , . Bookmark the permalink.

Leave a Reply