在客户端,HttpClient使用message handle处理request.默认的handler是HttpClientHandler,用来发送请求和获取response从服务端.可以在client pipline中插入自定义的message handler: 自定义的message handler: class MessageHandler1 : DelegatingHandler { private int _count = 0; protected override Task<Http…
Message Handlers是一个接收HTTP Request返回HTTP Response的类,继承自HttpMessageHandler 通常,一些列的message handler被链接到一起.第一个handler收到http request做一些处理,然后将request传递到下一个handler.在某时刻,response被创并返回.这种模式被称为delegating hanlder 服务端消息处理 在服务端,WebAPI管道使用一些内建的message handlers Http…
Authentication(认证)   WebAPI中的认证既可以使用HttpModel也可以使用HTTP message handler,具体使用哪个可以参考一下依据: 一个HttpModel可以检测ASP.NET请求管道中的所有请求,一个message handler仅仅可以检测到被路由到这个WebAPI的请求 可以预先设置message handlers,让特定的route使用指定的authentication scheme Http Module只能在IIS中使用,Message ha…
原文:Batching Handler for ASP.NET Web API 自定义实现HttpMessageHandler public class BatchHandler : HttpMessageHandler { HttpMessageInvoker _server; public BatchHandler(HttpConfiguration config) { _server = new HttpMessageInvoker(new HttpServer(config)); } p…
Cookie的几个参数: Domain.Path.Expires.Max-Age 如果Expires与Max-Age都存在,Max-Age优先级高,如果都没有设置cookie会在会话结束后删除cookie WebAPI中使用Cookie //写cookie public HttpResponseMessage Get() { var resp = new HttpResponseMessage(); var cookie = new CookieHeaderValue("session-id&q…
安装追踪用的包 Install-Package Microsoft.AspNet.WebApi.Tracing Update-Package Microsoft.AspNet.WebApi.WebHost //-Version指定具体的版本 启用追踪的功能(在WebApiConfig.cs中) public static class WebApiConfig { public static void Register(HttpConfiguration config) { SystemDiagn…
HttpResponseException 当WebAPI的控制器抛出一个未捕获的异常时,默认情况下,大多数异常被转为status code为500的http response即服务端错误. HttpResonseException是一个特别的情况,这个异常可以返回任意指定的http status code,也可以返回具体的错误信息. public Product GetProduct(int id) { Product item = repository.Get(id); if (item =…
BSON 是轻量级的,能够进行快速查询和高效的解码/编码.BSON方便查询是由于elements的前面都有一个表示长度的字段,所以解释器可以快速跳过这个elements:高效的解码/编码是因为numeric数据直接存储为numbers,不用转为string. 在服务端启用BSON   public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Formatter…
Web API内建支持XML.JSON.BSON.和form-urlencoded的MiME type. 创建的自定义MIME类型要继承一下类中的一个: MediaTypeFormatter 这个类使用异步的读/写方法 BufferedMediaTypeFormatter 这个类继承自MediaTypeFormatter,但它使用同步的读/写方法 自定义一个MIME类型,如下: public class ProductCsvFormatter:BufferedMediaTypeFormatter…
HTML Forms概述 <form action="api/values" method="post"> 默认的method是GET,如果使用GET,表单数据被编码到URI中作为查询字符串:如果使用POST,表单数据放在Request body中,enctype属性指定编码类型: 编码方式(enctype) 描述 application/x-www-form-urlencoded 表单数据被编码成name/value形式,默认的编码方式 multipa…