ASP.NET WebAPI 06 HttpMessageHandler管道
HttpMessageHandler管道
在Web API的中,微软为了更好的进行架构扩展,采用的了一套管道设计----HttpMessageHander(其实WCF也有类似架构).

在整个管道中的头与尾分别是HttpServer、HttpRoutingDispatcher,它们都继承HttpMessageHandler,其中ASP.NET 为我们预留了可扩展的DelegatingHandler,下面我们就来看看几个类之前的关系。
SendAsync为处理请求的方法。在DelegatingHandler中有一类型为HttpMessageHandler的属性InnerHandler。它是构成管道的关键。因为有了这个属性所以的DelegatingHandler都能够串联起来,逐步去经过管道中的每个DelegatingHandler
在HttpServer有两个新添加的属性Configuration与Dispatcher,其中Dispatcher指向管道的尾HttpRoutingDispatcher.在HttpConfiguration有一类型为Collection< DelegatingHandler>的属性MessageHandlers,我们这个集合中添加自定义的DelegatingHandler就可以添加到整个管道中。所以我们可以看出HttpServer中基本已经定义的整个HttpMessagHandler管道。
下面我们定义一个可以进行请求方式Post与Put(当然这只是个例子)
public class HttpMethodChangeHandler : DelegatingHandler
{
protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
var method = request.Method;
if(request.Method == HttpMethod.Post)
{
request.Method = HttpMethod.Put;
}
else if(request.Method == HttpMethod.Put)
{
request.Method = HttpMethod.Post;
}
return base.SendAsync(request, cancellationToken);
} }
我们在HttpApplication.Application_Start中加添加如下语句
GlobalConfiguration.Configure(t => t.MessageHandlers.Add(new HttpMethodChangeHandler()));
在DemoController中定义了Put,Post方法
[HttpPost]
public string Post()
{
return "这是一个Post方法";
} [HttpPut]
public string Put()
{
return "这是一个Put方法";
}
下面是这四种测试结果:
url:http://localhost:41666/api/Demo/Put Method:Post
结果:"这是一个Put方法"
url: http://localhost:41666/api/Demo/Put Method:Put
结果:{"Message":"请求的资源不支持 http 方法"POST"。"}
url:http://localhost:41666/api/Demo/Post Method:Put
结果:"这是一个Post方法"
url: http://localhost:41666/api/Demo/Post Method:Post
结果:{"Message":"请求的资源不支持 http 方法"PUT"。"}
另外我们再在DemoController定义一个获取自定义DelegatingHandler的方法GetChains
public IEnumerable<string> GetChains()
{ IEnumerable<string> result = GetChains(GlobalConfiguration.DefaultServer); return result;
} private IEnumerable<string> GetChains(DelegatingHandler handler)
{
yield return handler.GetType().FullName;
var innerHander = handler.InnerHandler as DelegatingHandler;
if (innerHander != null)
{
yield return innerHander.GetType().FullName;
} }

源码
Github: https://github.com/BarlowDu/WebAPI (API_6)
ASP.NET WebAPI 06 HttpMessageHandler管道的更多相关文章
- 细说Asp.Net WebAPI消息处理管道
我们在开发完Asp.Net WebAPI程序后,可以使用WebHost寄宿方式或者SelfHost寄宿方式来部署Asp.Net WebAPI.所谓WebHost寄宿就是通过Asp.Net来实现:所谓S ...
- ASP.NET WebAPI 14 仿写Filter管道
WebAPI中有设计了几种管道(Channel),大概如下:HttpMessageHandler,ActionFilter管道,ExceptionFilter管道.在三种管道中HttpMessageH ...
- Asp.Net WebApi核心对象解析(下篇)
在接着写Asp.Net WebApi核心对象解析(下篇)之前,还是一如既往的扯扯淡,元旦刚过,整个人还是处于晕的状态,一大早就来处理系统BUG,简直是坑爹(好在没让我元旦赶过来该BUG),队友挖的坑, ...
- ASP.NET WEBAPI 的身份验证和授权
定义 身份验证(Authentication):确定用户是谁. 授权(Authorization):确定用户能做什么,不能做什么. 身份验证 WebApi 假定身份验证发生在宿主程序称中.对于 web ...
- Enable Cross-Origin Requests in Asp.Net WebApi 2[Reprint]
Browser security prevents a web page from making AJAX requests to another domain. This restriction i ...
- 在ASP.NET WebAPI 中使用缓存【Redis】
初步看了下CacheCow与OutputCache,感觉还是CacheOutput比较符合自己的要求,使用也很简单 PM>Install-Package Strathweb.CacheOutpu ...
- ASP.NET WebAPI 08 Message,HttpConfiguration,DependencyResolver
ASP.NET WebAPI 08 Message,HttpConfiguration,DependencyResolver Message WebAPI作为通信架构必定包含包含请求与响应两个方法 ...
- ASP.NET WEBAPI 简单CURD综合测试(asp.net MVC,json.net,sql基础存储过程和视图,sqlhelper,json解析)
草图 真正的后端是不管前端是什么平台,用什么语言的,JSON格式的数据应该可以应对.用ASP.NET WEBAPI尝试做一个后端,实现最基本的CURD,业务逻辑和数据库操作都放在后端,前端只需要正 ...
- ASP.NET WebAPI 09 Controller的激活
在Controller之前我们先回顾一下HttpMessageHandler通道. 在这个图中我留一个HttpContollerDispatcher没有说明.因为这个类也是继承自HttpMessage ...
随机推荐
- 全能型eclipse 下载
百度网盘地址: 链接: https://pan.baidu.com/s/1RFUkdSZUfuubFATLVdVH3w 提取码:kncg 这个eclipse几乎包含了现在所有的开发语言
- virt-manager管理器新建虚拟机时出错:unsupported format character
启动管理器出错:unsupported format character ‘��0xffffffef) at index 30 经验证,解决办法如下: 1.获取virt-manager的rpm包,并强 ...
- python基础之装饰器(实例)
1.必备 #### 第一波 #### def foo(): print 'foo' foo #表示是函数 foo() #表示执行foo函数 #### 第二波 #### def foo(): print ...
- nodejs文件压缩-使用gulp命令(安装过程)
为了代码安全问题,一般发布程序的时候需要将js代码进行压缩,记录一下安装流程.避免忘记. 安装插件
- Web Api返回值
首先:注明,我还没这么强的功力啦!这是我看的网上的,因为怕博主删除就自己复制了一下 博主的网址是:http://www.cnblogs.com/landeanfen/p/5501487.html 使用 ...
- 关闭eclipse自动弹出console功能
使用eclipse时经常会用到最大化窗口,而如果此时是开着tomcat等服务的话,一段后台有打印什么东西出来都会自己弹出 console挺烦人的.可以使用以下操作关闭这个功能. Preferences ...
- Packet Tracer 5.0 构建CCNA实验(3)—— 路由器实现vlan间通信
--交换机配置 --交换机和路由器连接的接口 switchport mode trunk --路由器配置 enable conf t int fa0/0.1 --路由器接口的子接口 encapsula ...
- Anniversary party(树形dp入门)
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- phpcms取内容发布管理中的来源
调取内容发布管理中的来源,如果直接写{$val['copyfrom']}调取出来的内容为 内容|0 ,要先根据“|”进行拆分,然后再写. 示例: <!--新闻开始--> {pc:co ...
- C - Contest Setting Gym - 101982C dp 补题
题目链接:https://vjudge.net/contest/273260#problem/C 学习了一下别人的思路,首先去重,然后离散化. dp数组开二维,每一次更新,状态转移方程,dp[ i ] ...