Filter configurations

wald-tq edited this page Jul 17, 2015 · 8 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 MVC3 extension.

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

Further Information on this topic: