C# ASP.NET Web API ActionFilter 示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10307333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET Web API ActionFilter example
提问by WildeButNotOscar
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.
我是整个 MVC 的新手,正在考虑使用 ASP.NET Web API 重新实现一些 WCF 服务。作为其中的一部分,我想实现一个记录所有动作和异常以及计时的动作过滤器,所以我想我会从动作过滤器开始,但是没有调用过滤器。
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
{
...
}
The routes are setup in Global.asax using calls like:
使用以下调用在 Global.asax 中设置路由:
var routeTemplate = ...
var defaults = new { controller = controllerName, action = methodName };
var constraints = new { httpMethod = new HttpMethodConstraint(myHTTPMethods.Split(',')) };
routes.MapHttpRoute(methodName, routeTemplate, defaults, constraints);
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.
问题是 MyResourceController 上的操作按预期调用并成功运行。客户端能够向服务器查询必要的信息,并且一切正常,只是没有调用任何操作过滤器方法。
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?
我的理解是其余的事情“自动”发生。这显然是不够的 - 关于什么是错误的任何建议?我需要在某处注册这些吗?
回答by S P
You have to be sure your code uses the ActionFilterAttributefrom the System.Web.Http.Filtersnamespaceand not the one from System.Web.Mvc.
您必须确保您的代码使用ActionFilterAttributefromSystem.Web.Http.Filters命名空间而不是 from System.Web.Mvc。
So please check that you have
所以请检查你是否有
using System.Web.Http.Filters;
回答by Murali
As Sander mentioned I tried the below code, its action filter is getting executed.
正如 Sander 提到的,我尝试了下面的代码,它的动作过滤器正在执行。
public class WebAPIActionFilterAttribute : System.Web.Http.Filters.ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
PersonController.Messages.Add("OnActionExecuted");
}
public override void OnActionExecuting(HttpActionContext actionContext)
{
PersonController.Messages.Add("OnActionExecuting");
}
}
public class WebAPIExceptionFilter : System.Web.Http.Filters.ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
PersonController.Messages.Add("OnException");
actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent("Something went wrong") };
}
}
PersonController.Messages is a static string list. if you want to check whether OnActionExecuted is getting executed or not, you may call the same API method again, you would see the "OnActionExecuted" in the Messages list.
PersonController.Messages 是一个静态字符串列表。如果您想检查 OnActionExecuted 是否正在执行,您可以再次调用相同的 API 方法,您将在 Messages 列表中看到“OnActionExecuted”。

