Skip to content

Injection of filter attributes

Remo Gloor edited this page Feb 23, 2012 · 3 revisions

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. It is only there for backward compatibility. Here is an example of the log filter as filter attribute.

public class LogFilterAttribute : ActionFilterAttribute
{
    [Inject]
    public ILog Log
    {
        get; set;
    }
 
    public Level LogLevel
    {
        get; set;
    }
 
    public void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var message = string.Format(
                CultureInfo.InvariantCulture,
                "Executing action {0}.{1}",
                filterContext.ActionDescriptor.ControllerDescriptor.ControllerName,
                filterContext.ActionDescriptor.ActionName),
        this.Log.Logger.Log(typeof(LogFilter), this.LogLevel, message, null);
    }
 
    public void OnActionExecuted(ActionExecutedContext filterContext)
    {
        var message = string.Format(
                CultureInfo.InvariantCulture,
                "Executed action {0}.{1}",
                filterContext.ActionDescriptor.ControllerDescriptor.ControllerName,
                filterContext.ActionDescriptor.ActionName),
        this.Log.Logger.Log(typeof(LogFilter), this.LogLevel, message, null);
    }
}