This project is read-only.

How it works for ASP.NET MVC 2

FluentFilters helps you to implement a global filters functionality in your ASP.NET MVC application. A global filter is a filter that is going to run for every single action on every single controller. It is very simple to create a custom filter for usage globally. All you need to do is to implement your custom filter class with one or more interfaces which are the four types of ASP.NET MVC filters:
  • IAuthorizationFilter
  • IActionFilter
  • IResultFilter
  • IExceptionFilter
For more information about creating custom filter please review Creating a custom filter of project documentation.

For adding new filter you should use class FluentFiltersBuilder. This is the recommended usage if you build MVC application without IoC container. But if you use IoC container then recommended implement custom class who inherits FilterRegistry class and continue to work only with it. More detailed about it you can read here.

Basic usage:
// Register filter
With an IoC Container (Unity Application Block means in this example):
// Use custom class
IFilterRegistry registry = Container.Resolve<UnityFilterRegistry>();

// Register filter

// Register IFilterRegistry in Unity container
Later, when you added global filters for usage, you can set chain criteria for each of those. By criteria, you can set whether to execute filter or not. Create criteria is very simple too. You need to inherit your custom criteria class from IFilterCriteria interface and implement only method Match. For more information about creating custom criterion please review Creating a custom criteria of project documentation.

The library already provides three criteria for use:
  • ActionFilterCriteria - filter by specified action
  • AreaFilterCriteria - filter by specified area
  • ControllerFilterCriteria - filter by specified controller

For each filter, you can add two chains criteria. This is the criteria that is required and which should be excluded.

In the code below we added filter BrowserDetectionFilter for area "Blog" and excluded for controller "Account":
FluentFiltersBuilder.Current.Add<BrowserDetectionFilter>(c =>
    c.Require(new AreaFilterCriteria("Blog")); // Execute if current area "Blog"
    c.Exclude(new ControllerFilterCriteria("Account")); // Ignore if current controller "Account"
If criteria are not specified, the filter will be executed for entire application.
Chain of criteria you can build by methods And(IFilterCriteria criteria) and Or(IFilterCriteria criteria). Methods work as conditional logical operators && and ||.

Contrived example:
FluentFiltersBuilder.Current.Add<DisplayTopBannerFilter>(c =>
    c.Require(new IsFreeAccountFilterCriteria()).Or(new AreaFilterCriteria("Blog")).Or(new AreaFilterCriteria("Forum")).And(new IsMemberFilterCriteria());
    c.Exclude(new AreaFilterCriteria("Administrator")).Or(new ControllerFilterCriteria("Account")).And(new ActionFilterCriteria("LogOn"));
If speaking in C# language, then the code above can be understood as (like pseudocode):

if( IsFreeAccountFilterCriteria() || area == "Blog" || (area == "Forum" && IsMemberFilterCriteria()) ) 
    if(area != "Administrator")
    else if(controller != "Account" && action != "LogOn")

Last edited Jan 30, 2011 at 10:23 PM by banguit, version 67


No comments yet.