Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Introduction
This article explains about 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 type of filters in easy way with step by step process.
Definition
Filters are attribute that can be applied in controller level and action methods level. When we applied in controller level it is applicable for all actions with in controllers. Filters are used to add pre or post processing logic to action methods.
Background
Filter is one of the main advantages of ASP.NET MVC. It is used add extra advantaged of controllers and actions. We can add extra logic or process before or after execute controls and action methods. There are many default actions filters available in ASP.Net MVC. We can create our own filters base on our requirements. Action filters 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.
Step 2
Select MVC template from template window and click ok button.
Step 3
Go to solution explore, under solution explore right click on controller folder and add new controller like below screen shots.
After click controller, Controller window will open and select “MVC Controller- Empty” then click add button. After click add then enter controller Name and click ok.
** **
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 in inside of System.Web.Mvc assembly.
Now go to solution explore, select references. Under reference select “System.Web.Mvc” assembly.
After selecting System.Web.Mvc under reference in solution we can see all class and interfaces which are under System.Web.Mvc assembly. Below screenshot explains what are the class and interface available under ActionFilterAttributes.
ActionFiltersAttribute is abstract class. We can see all class and interface which are under ActionFiltersAttribute.
There many methods are available. Each method is important for Filters. If select a methods we can know about methods threw summary.
If know about one of method just select and read summary. We can understand about selected method. Above screenshot explains how to know about method.
Type of Filters
There four important action filters are in MVC. Already you what are the type of filter in MVC.
- Authorization Filter
- Action Filter
- Result Filters
- Exception Filter
Each Filter has it is own interface. Interface is a base of action filters. We can see the all interface under System.Web.Mvc. Below diagram shows all interface of Action Filters.
IAuthroizationFilter is an interface of authorization filters. It has an OnAuthorization() methods. Below diagram explains IAuthroizationFilter and it is method.
IActionFilter is an interface of action filters. OnActionExcuted() and OnActionExcuting() are methods of action filters. Below diagram explains IActionFilter and it is method.
IResultFilter is an interface of result filters. OnResultExcuted() and OnResultExcuting() are methods of result filter. Below diagram explains IResultFilter and it is method.
IExceptionFilter is an interface of exception filters. OnException() is methods of exception filter. Below diagram explains IExceptionFilter and it is method.
Step 4
Go to controller, right click and select add view. Below screen short explains how to add view.
Authorization Filters
Authorization filter is used to security purpose. Before enter controller and action Authorization attribute verifies authorized and authentication user.
Code for Authorized attribute
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVCfilters.Controllers
{
public class FilterController : Controller
{
// GET: Filter
[Authorize]
public ActionResult Index()
{
return View();
}
}
}
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.
Above screenshot explains about all authorization filters. If right on “IAuthorizationFilter” and select go to definition we can see the methods in corresponding interface.
All attributes are working based on oops concept. You can learn more details about authorization filters in following link.
Create identity in simple way using ASP.NET MVC 5
Conclusion
This article explains detailed knowledge of filters as well as simple way. It is useful to students and programmer those who are learning newly ASP.NET MVC. Next part of article explains other type of filters in ASP.NET MVC as well as how to create user defined filters in MVC.