Skip to content

Injection of filter attributes

remogloor edited this page Mar 31, 2012 · 1 revision

As already mentioned, the extension supports injection of filter attributes. But unlike as for the Ninject configured attributes it is restricted to property injection. That’s the reason why it is recommend not to use this feature. Here is an example of the log filter as filter attribute.

```text
public class LogFilterAttribute : ActionFilterAttribute
{
    [Inject]
    public ILog Log
    {
        get; set;
    }
 
    public Level LogLevel
    {
        get; set;
    }
 
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        this.log.DebugFormat(
            "Executing action {0}.{1}",
            actionContext.ActionDescriptor.ControllerDescriptor.ControllerName,
            actionContext.ActionDescriptor.ActionName);
    }

    public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
    {
        this.log.DebugFormat(
            "Executed action {0}.{1}",
            actionExecutedContext.ActionContext.ActionDescriptor.ControllerDescriptor.ControllerName,
            actionExecutedContext.ActionContext.ActionDescriptor.ActionName);
    }        
}
```