首先为什么说这是一个坑,是因为我们在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下的,知道这样了 就开始了改写过程....,运行调试,发现异常!!!

先看下异常代码:

 public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new PushFilter());
}
}
“System.InvalidOperationException”类型的异常在 System.Web.Mvc.dll 中发生,但未在用户代码中进行处理

其他信息: 给定的筛选器实例必须实现以下一个或多个筛选器接口: System.Web.Mvc.IAuthorizationFilter、System.Web.Mvc.IActionFilter、
System.Web.Mvc.IResultFilter、System.Web.Mvc.IExceptionFilter、System.Web.Mvc.Filters.IAuthenticationFilter。

这是为何呢。。。 明明就是这个过滤器,为什么还是会有异常? 原来问题在FilterConfig 这个类里面,这个类只是对MVC配置起效。(汗!!!!!!),我们加过滤器的代码要加入到webapi的配置而非mvc的配置,so 代码要这么写。

    public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "push.api.v1",
routeTemplate: "v1/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
); config.Filters.Add(new PushFilter());
}
}

陨石坑之webapi使用filter的更多相关文章

  1. 陨石坑之webapi 使用filter中如何结束请求流

    先看下正常的结束asp.net 请求流怎么写的 System.Web.HttpContext.Current.Response.Write(“end”); System.Web.HttpContext ...

  2. ASP.NET WebAPI 13 Filter

    Filter(筛选器)是基于AOP(面向方面编程)的设计,它的作用是Actionr的执行注入额外的逻辑,以达到横切注入的目的. IFilter 在WebAPI中所以的Filter都实现了IFilter ...

  3. 5.WebAPI的Filter

    1.WebApi的Filter介绍: 大家知道什么是AOP(aspect oriented programming)吗?它是可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添 ...

  4. Asp.Net WebAPI中Filter过滤器的使用以及执行顺序

    转发自:http://www.cnblogs.com/UliiAn/p/5402146.html 在WEB Api中,引入了面向切面编程(AOP)的思想,在某些特定的位置可以插入特定的Filter进行 ...

  5. webapi自定义Filter

    public class MyAutorFilter : IAuthorizationFilter { public bool AllowMultiple => true; public asy ...

  6. 在MVC或WEBAPI中记录每个Action的执行时间和记录下层方法调用时间

    刚才在博客园看了篇文章,http://www.cnblogs.com/cmt/p/csharp_regex_timeout.html  突然联想到以前遇到的问题,w3wp进程吃光CPU都挂起IIS进程 ...

  7. System.Web.Mvc 和 using System.Net.Http 的 Filter

    在尝试给webapi增加 ExceptionFilter时,出现了错误,经查询区别如下: System.Web.Mvc.Filters 是给mvc用的 System.Web.Http.Filters ...

  8. WebApi接口安全性 接口权限调用、参数防篡改防止恶意调用

    背景介绍 最近使用WebApi开发一套对外接口,主要是数据的外送以及结果回传,接口没什么难度,采用WebApi+EF的架构简单创建一个模板工程,使用template生成一套WebApi接口,去掉put ...

  9. django 操作数据库--orm(object relation mapping)---models

    思想 django为使用一种新的方式,即:关系对象映射(Object Relational Mapping,简称ORM). PHP:activerecord Java:Hibernate C#:Ent ...

随机推荐

  1. PHP+Mysql+jQuery实现文件下载次数统计

    数据表 CREATE TABLE IF NOT EXISTS `downloads` (   `id` int(6) unsigned NOT NULL AUTO_INCREMENT,   `file ...

  2. 利用jQuery内置的data()方法存储数据

    jQuery提供了内置的data()方法,与DOM元素不同的是,它可以用来存储key/value类型的数据.数据的存储是很容易的: $('#myDiv').data('currentState', ' ...

  3. 26 Time Management(转)

    01. There is alway time. Time is priorities. 时间常有.时间优先. 02. Days always fill up. 时间总会有的. Only plan f ...

  4. Linux下的tar压缩解压缩命令详解

    转载自http://www.cnblogs.com/qq78292959/archive/2011/07/06/2099427.html tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压 ...

  5. DataSnap 2009 系列之三 (生命周期篇)

    DataSnap 2009 系列之三 (生命周期篇) DataSnap 2009的服务器对象的生命周期依赖于DSServerClass组件的设置 当DSServer启动时从DSServerClass组 ...

  6. .Net字符串替换

    在.Net中,有些地方需要进行字符的替换才能实现一些相关功能,这里是一个简单的字符串替换的方法 //如下,变量strWhere中是通过一些方法获取的sql拼接的条件语句,但在数据库中是多表查询,有同名 ...

  7. ubuntu Unity Tweak Tool

    Unity Tweak Tool first install main program if do not run,so,second run : sudo apt-get install unity ...

  8. SQL Cumulative Sum累积求和

    期望结果:  ID  VAL  CumSum  1  10  10  2  20  30  3  30  60 方法一: 使用分析函数 select id,val,sum(val) over ( or ...

  9. linux grep -I 属性

    忽略大小写的查找: grep -i 'address' test.log --> address ADDRESS

  10. virtualbox 下windows与虚拟机实现文件共享---挂载

    1.创建挂载目录: mkdir /mnt/share 2.挂载:mount /dev/cdrom /mnt/share 3.cd /mnt/share 3.虚拟机-设备-分配光驱-VBoxGuestA ...