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 ...
随机推荐
- codeforces gym101243 A C D E F G H J
gym101243 A #include<iostream> #include<cstdio> #include<cmath> #include<cstrin ...
- BNU-2017.7.3排位赛1总结
比赛链接:https://www.bnuoj.com/v3/contest_show.php?cid=9146#info A题 国际象棋棋盘,黑白相间染色. B题 最大值只取决于每个连通块的大小,一个 ...
- 服务器上 tomcat 配置了 tomcat-users 但是还是 403 的问题
默认情况下,tomcat 限制了只能本机访问 如果我们想要修改这个设置: 编辑 webapps/manager/META-INF/context.xml <!--<Valve classN ...
- Qt ------ 设置透明度
void setWindowOpacity(qreal level); //设置所有控件的不透明度 setAttribute(Qt::WA_TranslucentBackground); // ...
- K8S dashboard 创建只读账户
1.创建名字为“Dashboard-viewonly“的Cluster Role,各种资源只给予了list,get,watch的权限.dashboard-viewonly.yaml --- apiVe ...
- Android 统一配置依赖管理
Android Studio中默认就是使用Gradle来构建管理工程的,当我们在工程构建过程中创建了多个Module时,就可能存在一个问题,那就是每个Module以及Module中一些公用库的依赖存在 ...
- 最小生成树的边的概念问题!!! 最小生成树的计数 bzoj 1016
1016: [JSOI2008]最小生成树计数 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 5292 Solved: 2163[Submit][St ...
- CF767 B. The Queue 贪心+细节
LINK 题意:一个业务开始时间为s,结束时间为f,一个人办护照的时间需要m分(如果在x时开始服务,且x+m==f那么还是合法的),你可以选择任意时间到达,但如果你和其他人同时到达,你要排在他的后面. ...
- [php]禁用缓存
header("Expires: -1"); header("Cache-Control: no_cache"); header("pragma: n ...
- 查询timestamp类型数据
$where=" roleid = 8 and lizhi = 0 and branchid IN (".implode(",",$ids).") a ...