w3resource

Filter Registration


In the previous tutorial, we introduced Filters in ASP.NET MVC application. In this section we will look at filter registration and how to use these registered filters in our application.

In an ASP.NET MVC application, filters can be registered or applied in three levels

  1. Global Level
  2. Controller Level
  3. Action Method Level

Global Level

Filters can be applied at a global level in an ASP.NET MVC application in the Application_Start method of the Global.asax file by using default FilterConfig.RegisterGlobalFilters() method. With this, global filters will be applied to all the controller and action methods of an application.

The [HandleError] filter by default is applied globally in an MVC Application created using Visual Studio as shown below in the code snippet below:

//Registration of Global Filters
// MvcApplication class contains in Global.asax file  
public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    }
}
// FilterConfig.cs located in App_Start folder 
public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }
}

Controller level

When filters are applied at the controller level, they become applicable to all the action method of Controller class. For example, in the code snippet below, the [HandleError] filter applied to the ActivitiesController will automatically be inherited by the index method of the ActivitiesController.

Example: Applying Filters on Controller

[HandleError]
public class ActivitiesController: Controller
{
    public ActionResult Index()
    {
        return View();
    }

}

Action method level

You can apply filters to an individual action method also. So, filter will be applicable to that particular action method only.

Example: Applying Filters on Action Method

public class ActivitiesController: Controller
{
    [HandleError]
    public ActionResult Index()
    {
        return View();
    }

}

Multiple built in filters can be applied globally, at the controller or at the action method level. Some of these built in filters includes:[Authorize], [RequireHttps], [ChildActionOnly], [OutputCache] and [HandleError].

Filter Order

s mentioned above, MVC includes different types of filters and multiple filters can be applied to a single controller class or action method. So, filters when multiple filters are applied, they run in the following order.

  1. Authorization filters
  2. Action filters
  3. Response filters
  4. Exception filters

Create Custom Filter

You can create custom filter attributes by implementing an appropriate filter interface for which you want to create a custom filter and also derive a FilterAttribute class so that you can use that class as an attribute.

For example, implement IExceptionFilter and FilterAttribute class to create custom exception filter. In the same way implement an IAuthorizatinFilter interface and FilterAttribute class to create a custom authorization filter.

Example: Custom Exception Filter

class MyCustomErrorHandler: FilterAttribute, IExceptionFilter
{
    public override void IExceptionFilter.OnException(ExceptionContext filterContext)
    {
        Log(filterContext.Exception);

        base.OnException(filterContext);
    }

    private void Log(Exception exception)
    {
        //log exception here..
 
    }
}

Alternatively, you can also derive a built-in filter class and override an appropriate method to extend the functionality of built-in filters.

Let's create custom exception filter to log every unhandled exception by deriving built-in HandleErrorAttribute class and overriding OnException method as shown below.

//Example: Custom Exception Filter
class MyCustomErrorHandler : HandleErrorAttribute
{
    public override void OnException(ExceptionContext filterContext)
    {
        Log(filterContext.Exception);

        base.OnException(filterContext);
    }

    private void Log(Exception exception)
    {
        //log exception here..
 
    }
}

Now, you can apply MyCustomErrorHandler attribute at global level or controller or action method level, the same way we applied the HandleError attribute.

Example: Custom Action Filters to Controller

[MyCustomErrorHandler]
public class ActivitiesController: Controller
{
    public ActionResult Index()
    {
        return View();
    }

}


Inviting useful, relevant, well-written and unique guest posts