//执行action前执行这个方法 public override void OnActionExecuting(HttpActionContext filterContext) { //用于计算消耗时间 var stopWatch = new Stopwatch(); filterContext.Request.Properties[Key] = stopWatch; stopWatch.Start(); }
//执行action后执行这个方法 public override void OnActionExecuted(HttpActionExecutedContext filterContext) { var stopWatch = filterContext.Request.Properties[Key] as Stopwatch; if (stopWatch != null) { stopWatch.Stop(); TimeSpan time = stopWatch.Elapsed; } }