HTTP Message Handlers in ASP.NET Web API
https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/http-message-handlers
A message handler is a class that receives an HTTP request and returns an HTTP response. Message handlers derive from the abstract HttpMessageHandler class.
Typically, a series of message handlers are chained together. The first handler receives an HTTP request, does some processing, and gives the request to the next handler. At some point, the response is created and goes back up the chain. This pattern is called a delegating handler.

Server-Side Message Handlers
On the server side, the Web API pipeline uses some built-in message handlers:
- HttpServer gets the request from the host.
- HttpRoutingDispatcher dispatches the request based on the route.
- HttpControllerDispatcher sends the request to a Web API controller.
You can add custom handlers to the pipeline. Message handlers are good for cross-cutting concerns that operate at the level of HTTP messages (rather than controller actions). For example, a message handler might:
- Read or modify request headers.
- Add a response header to responses.
- Validate requests before they reach the controller.
This diagram shows two custom handlers inserted into the pipeline:

Note:On the client side, HttpClient also uses message handlers. For more information, see HttpClient Message Handlers.
Custom Message Handlers
To write a custom message handler, derive from System.Net.Http.DelegatingHandler and override the SendAsync method. This method has the following signature:
Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken);
The method takes an HttpRequestMessage as input and asynchronously returns an HttpResponseMessage. A typical implementation does the following:
- Process the request message.
- Call
base.SendAsyncto send the request to the inner handler. - The inner handler returns a response message. (This step is asynchronous.)
- Process the response and return it to the caller.
Here is a trivial example:
HTTP Message Handlers in ASP.NET Web API的更多相关文章
- ASP.NET Web API之消息[拦截]处理
标题相当难取,内容也许和您想的不一样,而且网上已经有很多这方面的资料了,我不过是在实践过程中作下记录.废话少说,直接开始. Exception 当服务端抛出未处理异常时,most exceptions ...
- 【ASP.NET Web API教程】5.1 HTTP消息处理器
原文:[ASP.NET Web API教程]5.1 HTTP消息处理器 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本系列教程,请先看前面的内容. 5.1 HTTP ...
- 杂项:ASP.NET Web API
ylbtech-杂项:ASP.NET Web API ASP.NET Web API 是一种框架,用于轻松构建可以访问多种客户端(包括浏览器和移动设备)的 HTTP 服务. ASP.NET Web A ...
- ASP.NET Web API的安全管道
本篇体验ASP.NET Web API的安全管道.这里的安全管道是指在请求和响应过程中所经历的各个组件或进程,比如有IIS,HttpModule,OWIN,WebAPI,等等.在这个管道中大致分两个阶 ...
- Lifecycle of an ASP.NET Web API Message
ASP.NET Web API, as we know now, is a framework that helps build Services over HTTP. Web API was int ...
- Custom Exception in ASP.NET Web API 2 with Custom HttpResponse Message
A benefit of using ASP.NET Web API is that it can be consumed by any client with the capability of m ...
- ASP.NET Web API系列教程目录
ASP.NET Web API系列教程目录 Introduction:What's This New Web API?引子:新的Web API是什么? Chapter 1: Getting Start ...
- 【ASP.NET Web API教程】3.2 通过.NET客户端调用Web API(C#)
原文:[ASP.NET Web API教程]3.2 通过.NET客户端调用Web API(C#) 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的 ...
- 【ASP.NET Web API教程】3 Web API客户端
原文:[ASP.NET Web API教程]3 Web API客户端 Chapter 3: Web API Clients 第3章 Web API客户端 本文引自:http://www.asp.net ...
随机推荐
- Redis 连接失败redis Can't init enough connections amount!
Can't init enough connections amount! Only 0 from 10 were initialized. Server: IP:6379 无法初始化足够的连接数量! ...
- iOS-将NSString转换成UTF8编码的NSString
在使用网络地址时,一般要先将url进行encode成UTF8格式的编码,否则在使用时可能报告网址不存在的错误,这时就需要进行转换 下面就是转换函数: NSString *urlString= [NSS ...
- spring中关于FeignClient的错误 QueryParam.value() was empty on parameter 1
现创建一个feignClient的接口,在其他服务调用改client时发现服务启动失败错误日志如下: org.springframework.beans.factory.BeanCreationExc ...
- iOS 程序切换后台
1. -(void)animationFinished:(NSString*)animationid finished:(NSNumber*)finished context:(void*)conte ...
- Less-css预处理Node and VS扩展编译
node编译 第一步:https://nodejs.org/en/ 到node官网下载最新的node 第二步:和普通软件一样把node安装好 第三步:运行-cmd,准备安装less 全局安装(整个电 ...
- instanceof和isInstance(Object obj) 和isAssignableFrom(Class cls)的区别和联系
instanceof和isInstance(Object obj) 和isAssignableFrom(Class cls)的区别和联系 编程的时候可能会遇到一个不知道它属于哪个类的 ...
- Oracle存储——逻辑结构
Oracle 数存储——物理结构 Oracle存储结构:物理结构+逻辑结构 Oracle 数据库存储逻辑结构 Oracle Schema Objects(Schema Object Storage A ...
- ORACLE的测试用户Scott
Oracle数据库的测试用户Scott的密码为什么是Tiger? 1977年6月,Larry Ellison 与 Bob Miner 和 Ed Oates 在硅谷共同创办了一家名为软件开发实验室(So ...
- 【Python之路】第十二篇--JavaScript
JavaScript 历史 1992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在CEnvi软件中).后将其改名ScriptEase.(客户端执行的语言) Net ...
- dist\_wepylogs.js
console.log('WePY开启错误监控'); console.warn("CLI报错:WARNING IN : src\pages\cloundAd.vue\n[xmldom war ...