码迷,mamicode.com
首页 > Windows程序 > 详细

WebApi2官网学习记录---Content Negotiation

时间:2015-04-24 13:52:08      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

Content Negotiation的意思是:当有多种Content-Type可供选择时,选择最合适的一种进行序列化并返回给client。

主要依据请求中的Accept、Accept-Charset、Accept-Encoding、Accept-Language这些属性决定的,但也会查看其它属性

如,如果请求中包含“ X-Requested-With”(ajax请求),在没哟其它Accept时,则使用JSON。

Content Negotiation的工作原理

首先,pipeline从HttpConfiguration 对象中获得IContentNegotiator 服务,同时也会从 HttpConfiguration.Formatters中获得所有的media formatters 。接下来,pipeline调用 IContentNegotiatior.Negotiate传入一下值

  • 要序列化的对象
  • 所有media formatters
  • HTTP Request

Negotiate 返回2个值:

  • 选择的formatter
  • 要输出的media type

如果没有合适的formatter,Negotiate方法返回null,client会收到406错误(无法访问)

下面是一个controller直接使用content negotiation的例子“

技术分享
 1 public HttpResponseMessage GetProduct(int id)
 2 {
 3     var product = new Product() 
 4         { Id = id, Name = "Gizmo", Category = "Widgets", Price = 1.99M };
 5 
 6     IContentNegotiator negotiator = this.Configuration.Services.GetContentNegotiator();
 7 
 8     ContentNegotiationResult result = negotiator.Negotiate(
 9         typeof(Product), this.Request, this.Configuration.Formatters);
10     if (result == null)
11     {
12         var response = new HttpResponseMessage(HttpStatusCode.NotAcceptable);
13         throw new HttpResponseException(response));
14     }
15 
16     return new HttpResponseMessage()
17     {
18         Content = new ObjectContent<Product>(
19             product,                // What we are serializing 
20             result.Formatter,           // The media formatter
21             result.MediaType.MediaType  // The MIME type
22         )
23     };
24 }
View Code

 默认的Content Negotiator

  DefaultContentNegotiator类是IContentNegotiator的默认实现,它使用以下标准选择formatter。

  首先,这个类型必须能够序列化,这步验证通过MediaTypeFormatter.CanWriteType来实现

  其次, content negotiator查看每个formatter并计算最与Http请求的formatter。匹配的原则如下:

  • SupportedMediaTypes 集合中包括所有的被支持的media type, content negotiator 使用Accept header与这个集合匹配。

         注意,Accept Header可以是一个范围。例如,“text/plain”是text/*或"/"的匹配项

  • MediaTypeMappings 集合包含一系列的MediaTypeMappings 对象,它提供了http request与某种media type的匹配。如,

         可以将自定义的Http Header与指定的media type对应

     如果根据Accept Header没有找到合适的匹配项,ontent negotiator会尝试根据request body匹配media type。例如,如果请求包含JSON数据,content negotiator将使用 JSON formatter。

     如果此时还没找到合适的匹配项,content negotiator会使用第一个可以序列化这个对象的formatter。

     当formatter确定好后, content negotiator会查看这个formatter支持的SupportedEncodings ,并与 Accept-Charset header 进行匹配,查到最合适的 character encoding。

                             

WebApi2官网学习记录---Content Negotiation

标签:

原文地址:http://www.cnblogs.com/goodlucklzq/p/4451609.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!