陨石坑之webapi使用filter
首先为什么说这是一个坑,是因为我们在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的更多相关文章
- 陨石坑之webapi 使用filter中如何结束请求流
先看下正常的结束asp.net 请求流怎么写的 System.Web.HttpContext.Current.Response.Write(“end”); System.Web.HttpContext ...
- ASP.NET WebAPI 13 Filter
Filter(筛选器)是基于AOP(面向方面编程)的设计,它的作用是Actionr的执行注入额外的逻辑,以达到横切注入的目的. IFilter 在WebAPI中所以的Filter都实现了IFilter ...
- 5.WebAPI的Filter
1.WebApi的Filter介绍: 大家知道什么是AOP(aspect oriented programming)吗?它是可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添 ...
- Asp.Net WebAPI中Filter过滤器的使用以及执行顺序
转发自:http://www.cnblogs.com/UliiAn/p/5402146.html 在WEB Api中,引入了面向切面编程(AOP)的思想,在某些特定的位置可以插入特定的Filter进行 ...
- webapi自定义Filter
public class MyAutorFilter : IAuthorizationFilter { public bool AllowMultiple => true; public asy ...
- 在MVC或WEBAPI中记录每个Action的执行时间和记录下层方法调用时间
刚才在博客园看了篇文章,http://www.cnblogs.com/cmt/p/csharp_regex_timeout.html 突然联想到以前遇到的问题,w3wp进程吃光CPU都挂起IIS进程 ...
- System.Web.Mvc 和 using System.Net.Http 的 Filter
在尝试给webapi增加 ExceptionFilter时,出现了错误,经查询区别如下: System.Web.Mvc.Filters 是给mvc用的 System.Web.Http.Filters ...
- WebApi接口安全性 接口权限调用、参数防篡改防止恶意调用
背景介绍 最近使用WebApi开发一套对外接口,主要是数据的外送以及结果回传,接口没什么难度,采用WebApi+EF的架构简单创建一个模板工程,使用template生成一套WebApi接口,去掉put ...
- django 操作数据库--orm(object relation mapping)---models
思想 django为使用一种新的方式,即:关系对象映射(Object Relational Mapping,简称ORM). PHP:activerecord Java:Hibernate C#:Ent ...
随机推荐
- VUE 入门基础(8)
十,组件 使用组件 注册 可以通过以下这种方式创建一个Vue实例 new Vue({ el: '#some-element', }) 注册一个全局组件,你可以使用Vue.component(tagNa ...
- 【转】Selenium 面试题总结(乙醇Blog记录的面试题)
原文链接:http://www.cnblogs.com/tsbc/p/4922368.html ###selenium中如何判断元素是否存在? - isElementPresent ###sele ...
- python Tornado(招聘的一个比较经常问到的知识)
Tornado既是一个webserver也是一个web框架 这是一个总结的比较详细的内容 http://www.nowamagic.net/academy/detail/1332612 开源中国中的关 ...
- 今天使用bootstrap中的tagsinput控件,碰到个小问题
我直接使用时,后台一直报Uncaught Can't add objects when itemValue option is not set错误, 代码如下: <div> <inp ...
- c#利用反射获取对象属性值
public static string GetObjectPropertyValue<T>(T t, string propertyname){ Type type = type ...
- 关于初学者Could not find action or result :No result defined for action com.lyw.action.LoginAction and result success
解决如下: 将:<package name="struts2" extends="struts-default" > <action ...
- 误差曲线(Matlab)
loglog(dof(:),errorestimate(:),:),errorestimate1(:),);hold on; loglog(dof(:),eigvalue(:)-[);%[13.086 ...
- Java file read & write
1. read public static void readfile(String filepath) { BufferedReader br = null; try { String sCurre ...
- Glide 加载图片
//通过model获取到图片的url,将Url转换成bitmap对象: //设置不保存内存和硬盘缓存, 1 Glide.with(mContext).load(model.getVideoUrl()) ...
- 第15章 .NET中的反射
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...