首先为什么说这是一个坑,是因为我们在webapi中使用filter的时候也许会先百度一下,好吧,挖坑的来了,我看了好几篇文章写的是使用System.Web.Mvc.Filters.ActionFilterAttribute。
然后开始痛苦的调试,发现这个过滤器永远调不进来(windows azure mobile services除外)。then.... 还是Google吧 !
痛苦后才懂,原来不是这么一回事,ActionFilterAttribute 有2个不同的版本,一个在System.Web.Mvc空间下,另外一个则在System.Web.Http.Filters命名空间下。他们有何区别?
The System.Web.Http one is for Web API; the System.Web.Mvc one is for previous MVC versions.
You can see from the source that the Web API version has several differences.
好吧,原来System.Web.Mvc.Filters.ActionFilterAttribute是给mvc用的,我们要用System.Web.Http.Filters下的,知道这样了 就开始了改写过程....,运行调试,发现异常!!!
先看下异常代码:
1 public class FilterConfig
2 {
3 public static void RegisterGlobalFilters(GlobalFilterCollection filters)
4 {
5 filters.Add(new HandleErrorAttribute());
6 filters.Add(new PushFilter());
7 }
8 }