Web API:'全局'过滤器不起作用(ExceptionFilter)

use*_*899 11 exception-handling global asp.net-web-api

我在这里实现了异常过滤器:http://www.asp.net/web-api/overview/web-api-routing-and-actions/exception-handling 并在全球注册,如microsoft或stackoverflow-users(如何添加全球ASP.Net Web Api过滤器?)解释.

public static void RegisterWebApiFilters(System.Web.Http.Filters.GlobalFilterCollection filters)
{
//other filters
  filters.Add(new MyExceptionFilter());
}
Run Code Online (Sandbox Code Playgroud)

但是如果我抛出异常,我的方法就不会被调用.只有当我将属性[MyExceptionFilter]添加到controller-method时才会调用我的异常处理方法,但我希望通过全局注册过滤器来避免所有方法.

我试图为过滤器设置订单,但这没有效果.


编辑:我注意到,在新的Wep Api RC中,该方法称为"RegisterGlobalFilters",这似乎是MVC过滤器集合.

如果我打电话

GlobalConfiguration.Configuration.Filters.Add(new MyExceptionFilter());
Run Code Online (Sandbox Code Playgroud)

有用.这是Web Api的集合.

看起来我必须为web api构建我自己的"FilterConfig"类...

use*_*899 20

就像我在我的问题中提到的:有不同的过滤器集合.一个用于MVC,一个用于web api.

如果要将过滤器添加到web api,请将此行代码添加到global.asax

GlobalConfiguration.Configuration.Filters.Add(new MyExceptionFilter());
Run Code Online (Sandbox Code Playgroud)