Skip to content

Filter configurations

Sean Salmon edited this page Mar 6, 2015 · 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.

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

Further Information on this topic: