Detailed Knowledge Of Filters In ASP.NET MVC 5 In Step By Step Process: Part One

Introduction

This article explains how to create custom filters and user defined filters in ASP.NET MVC 5, in step by step way. There are different types of action filters in MVC and this article explains each.

Definition

Filter is  an attribute that can be applied in controller level and action methods level. When applied in controller level, it is applicable for all actions with in controllers. Filters are used to add pre or post processing logic to the action methods.

Background

Filter is one of the main advantages of ASP.NET MVC. It is used to add extra advantage of controllers and actions. We can add extra logic or process before or after executing the controls and action methods. There are many default action filters available in ASP.NET MVC. We can create our own filters base on our requirement. Action filter is a special methodology in MVC inject peace of code or logic. Namespace for filters is “System.Web.Mvc”. Base class for filters is “FilterAttribute”.

Steps for creating default filters:

Step 1: Go to Visual Studio. Open new ASP.NET Web Application and give useful project name. Follow the below screenshot.

new ASP.Net Web Application

Step 2: Select MVC template from template window and click OK button.

MVC template

Step 3: Go to Solution Explorer. Right click on controller folder and add new controller, as shown in the below screenshot.

solution explore

After clicking controller, Controller window will open. Select “MVC Controller- Empty” and click Add button. Enter the Controller Name and click OK.


controller

Basic Details of Filters in MVC

Core of filters in ASP.NET MVC are System.Web.Mvc assembly and System.Web.Mvc.ActionFilterAttribute abstract class. ActionFilterAttribute is located inside of the System.Web.Mvc assembly.

Now, go to Solution Explorer, select References. Under Reference, select “System.Web.Mvc” assembly.

solution explore

We can see all classes and interfaces lying under System.Web.Mvc assembly. The following screen explains what are the classes and interfaces available under ActionFilterAttributes.

ActionFilterAttributes

ActionFiltersAttribute is abstract class. We can see all classes and interfaces which are under ActionFiltersAttribute.

Several methods are available there. Each method is important for filters. If selected a method, we can know about it through its summary.

methods

Type of Filters

There four important action filters in MVC.

  1. Authorization Filter
  2. Action Filter
  3. Result Filters
  4. Exception Filter

Each Filter has it is own interface. Interface is a base of action filters. We can see all the interfaces under System.Web.Mvc. The below diagram shows all interfaces of Action Filters.

IAuthroizationFilter is an interface of authorization filters. It has an OnAuthorization() method.

IAuthroizationFilter

IActionFilter is an interface of action filters. OnActionExcuted() and OnActionExcuting() are methods of action filters.

IActionFilter

IResultFilter is an interface of result filters. OnResultExcuted() and OnResultExcuting() are methods of result filter. 

IResultFilter

IExceptionFilter is an interface of exception filters. OnException() is methods of exception filter. 

IExceptionFilter

Step 4: Go to Controller, right click and select Add View. The below screenshot explains how to add View.

controller

Authorization Filters

Authorization filter is used for security purpose. Before entering the controller and action, the authorization attribute verifies the authorized and authenticated user.

Code for Authorized attribute

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. namespace MVCfilters.Controllers {  
  7.     public class FilterController: Controller {  
  8.         // GET: Filter  
  9.         [Authorize]  
  10.         public ActionResult Index() {  
  11.             return View();  
  12.         }  
  13.     }  
  14. }  
If right click on authorization attribute and click go to definition, we can see the “AuthorizeAttribute” class. “FilterAttribute” and “IAuthorizationFilter” are core and inherited in “AuthorizeAttribute” class.

AuthorizeAttribute

AuthorizeAttribute
All attributes are working based on OOPS concept. You can learn more in details about authorization filters on the following link.

 

Conclusion

This article gives the detailed knowledge of filters. It is useful for students and novice programmer who have recently started learning the ASP.NET MVC. 


Similar Articles