I'm new to all MVC work, and I am looking at reimplementing some WCF services using the ASP.NET Web API. As part of this, I would like to implement an action filter that logs all actions and exceptions, and also makes a timing, so I thought I would start with an action filter, but the filter is not called.
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); }
}
and on the controller I have
[MyTrackingActionFilter] public class MyResourceController : ApiController { ... }
Routes are set in Global.asax using calls such as:
var routeTemplate = ... var defaults = new { controller = controllerName, action = methodName }; var constraints = new { httpMethod = new HttpMethodConstraint(myHTTPMethods.Split(',')) }; routes.MapHttpRoute(methodName, routeTemplate, defaults, constraints);
The problem is that the actions in MyResourceController invoke, as expected, and succeed. A client can request a server to obtain the necessary information, and everything behaves well, except that none of the action filter methods are called.
I realized that everything else happened "automatically." Honestly, this is not enough - Any judgments about what is wrong? Do I need to register this?
asp.net-web-api
WildeButNotOscar Apr 24 '12 at 23:19 2012-04-24 23:19
source share