首页 技术 正文
技术 2022年11月12日
0 收藏 667 点赞 4,575 浏览 1931 个字

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。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,071
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,549
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,397
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,174
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,809
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,889