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

     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。

                                 

  • 相关阅读:
    Math对象
    MDN中的对象原型
    构造函数的静态成员和实例成员
    js对象的九大特点
    对象数据的使用方法
    创建对象的所有方式
    Linux下gcc编译器的使用
    Linux vim环境设置
    Linux下is not in the sudoers file解决方法
    mySQL相关函数的使用
  • 原文地址:https://www.cnblogs.com/goodlucklzq/p/4451609.html
Copyright © 2020-2023  润新知