Skip to content

Filter configurations

remogloor edited this page Mar 31, 2012 · 3 revisions

In the previous examples you have already seen that a filter can be configured by adding one or more With statements to the binding configuration. But some times it is necessary to have different configurations for different actions. In this case it’s possible to get the configuration value from an attribute of the action or controller. The next example shows all new With overloads that come with the WebAPI extension.

```text
[Log(LogLevel = Level.Debug)]
void Index() {}
 
this.BindHttpFilter(FilterScope.Controller)
     .WhenControllerHas()
     .WithConstructorArgumentFromControllerAttribute(
          "logLevel",
          attribute => attribute.LogLevel);
// For property injection WithPropertyValueFromControllerAttribute instead
 
this.BindHttpFilter(FilterScope.Action)
     .WhenActionHas()
     .WithConstructorArgumentFromActionAttribute(
          "logLevel",
          attribute => attribute.LogLevel);
// For property injection WithPropertyValueFromActionAttribute instead
 
this.BindHttpFilter(FilterScope.Action)
     .WhenActionHas()
     .WithConstructorArgument((
          "logLevel",
          (context, controllerContext, actionDescriptor) =>
               actionDescriptor.ActionName == "Index" ? Level.Info : Level.Warn);
```

Further Information on this topic: