title: ASP.NET Web API ActionFilter example
body: <p>I'm new to the whole MVC thing and am looking at re-implementing some WCF services using ASP.NET  Web API. As part of that, I'd like to implement an action filter that logs all actions and exceptions as well as does timing so I thought I'd start with an Action Filter, however the filter is not being invoked. </p>
  
  <pre><code>public class MyTrackingActionFilter : ActionFilterAttribute, IExceptionFilter 
  {
      private Stopwatch stopwatch = new Stopwatch();
  
      public void OnException(ExceptionContext filterContext)
      {
             ...
      }
  
      public override void OnActionExecuted(ActionExecutedContext filterContext)
      {
             ...
      }
  
      public override void OnActionExecuting(ActionExecutingContext filterContext)
      {
          this.stopwatch.Start();
          Trace.TraceInformation(" Entering {0}", filterContext.RouteData);
      }
  }
  </code></pre>
  
  <p>and on the controller, I have</p>
  
  <pre class="lang-cs prettyprint-override"><code>[MyTrackingActionFilter]
  public class MyResourceController : ApiController
  {
    ...
  }
  </code></pre>
  
  <p>The routes are setup in Global.asax using calls like:</p>
  
  <pre class="lang-cs prettyprint-override"><code>var routeTemplate = ...
  var defaults = new { controller = controllerName, action = methodName };
  var constraints = new { httpMethod = new HttpMethodConstraint(myHTTPMethods.Split(',')) };
  
  routes.MapHttpRoute(methodName, routeTemplate, defaults, constraints);
  </code></pre>
  
  <p>The issue is that the actions on the MyResourceController are invoked as expected and run successfully. The client is able to query the server for the necessary info and all behaves fine, except that none of the action filter methods are ever invoked.</p>
  
  <p>My understanding was that the rest happened "automagically". That's clearly not enough - Any sugestions as to what is wrong? Do I need to register these somewhere?</p>