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

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的例子“

 public HttpResponseMessage GetProduct(int id)
{
var product = new Product()
{ Id = id, Name = "Gizmo", Category = "Widgets", Price = 1.99M }; IContentNegotiator negotiator = this.Configuration.Services.GetContentNegotiator(); ContentNegotiationResult result = negotiator.Negotiate(
typeof(Product), this.Request, this.Configuration.Formatters);
if (result == null)
{
var response = new HttpResponseMessage(HttpStatusCode.NotAcceptable);
throw new HttpResponseException(response));
} return new HttpResponseMessage()
{
Content = new ObjectContent<Product>(
product, // What we are serializing
result.Formatter, // The media formatter
result.MediaType.MediaType // The MIME type
)
};
}

默认的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。

上一篇:Linux服务器安全小知识


下一篇:expect 安装 salt 客户端