在我的应用程序中,我向Api控制器发出了一些JavaScript请求,以获得一些html格式的字符串。 当这些请求使用Accept: */* HTTP头(jQuery $.get方法)进行时,默认情况下使用JsonMediaTypeFormatter ,数据以JSON格式的Content-Type: application/json 。
我想要的是将*/*请求作为text/html 。 所以我试图创建一个支持*/*媒体类型的自定义MediaTypeFormatter ,但它给了我以下错误
*/*的'MediaTypeHeaderValue'不能用作支持的媒体类型,因为它是一个媒体范围。
或者,我总是可以在我的请求中提供正确的预期数据类型,但我很好奇是否有办法处理*/*媒体类型。
In my application I'm making some javascript requests to my Api Controllers to get some html formatted strings. When those requests are made with Accept: */* HTTP header (jQuery $.get method), so by default JsonMediaTypeFormatter is used and the data is returned with Content-Type: application/json in JSON format.
What I would like is to handle */* requests as text/html. So I tried to create a custom MediaTypeFormatter that supports */* media type, but it gives me the following error
The 'MediaTypeHeaderValue' of */* cannot be used as a supported media type because it is a media range.`
Alternatively I could always provide correct expected data types in my requests, but I'm curious if there's a way to handle */* media types.
最满意答案
上述行为是由于以下原因:
Web API中的默认con-neg算法具有以下选择响应格式化程序的优先顺序 :
基于媒体类型映射的格式符匹配。 基于请求接受标头的媒体类型的格式符匹配。 格式符匹配基于请求内容类型头的媒体类型。 格式化程序匹配是否可以序列化响应数据的类型。现在,JsonMediaTypeFormatter带有一个名为XmlHttpRequestHeaderMapping的内置媒体类型映射,该映射检查传入的请求并查看请求是否具有x-requested-with: XMLHttpRequest的头部x-requested-with: XMLHttpRequest并且如果没有accept头部或者只有Accept头部*/* 。
由于您的请求大多可能看起来像下面,根据优先顺序JsonMediaTypeFormatter被选为写回应的那个:
GET /api/something Accept: */* x-requested-with: XMLHttpRequest
你的问题的解决方案是明确要求“文本/ HTML”,因为这是你所期待的。
GET /api/something Accept: text/html x-requested-with: XMLHttpRequest
我写了很多关于内容协商的非常老的博客文章: http : //blogs.msdn.com/b/kiranchalla/archive/2012/02/25/content-negotiation-in-asp-net-mvc4-web-api -β-部分1.aspx http://blogs.msdn.com/b/kiranchalla/archive/2012/02/27/content-negotiation-in-asp-net-mvc4-web-api-beta-part-2.aspx
The above behavior is due to the following:
The default con-neg algorithm in Web API has the following precedence order of choosing the formatter for response:
Formatter match based on Media Type Mapping. Formatter match based on Request Accept header's media type. Formatter match based on Request Content-Type header's media type. Formatter match based on if it can serialize the response data’s Type.Now, JsonMediaTypeFormatter comes with a built-in media type mapping called XmlHttpRequestHeaderMapping which inspects an incoming request and sees if the request has the header x-requested-with: XMLHttpRequest and also if there is no accept header or if the Accept header is only having */*.
Since your request is mostly probably looking like below, according to the precedence order JsonMediaTypeFormatter is chosen as the one writing the response:
GET /api/something Accept: */* x-requested-with: XMLHttpRequest
A solution for your issue would be is to explicitly ask for "text/html" as this is what you are expecting.
GET /api/something Accept: text/html x-requested-with: XMLHttpRequest
Couple of very old blog posts about Content negotiation that I wrote: http://blogs.msdn.com/b/kiranchalla/archive/2012/02/25/content-negotiation-in-asp-net-mvc4-web-api-beta-part-1.aspx http://blogs.msdn.com/b/kiranchalla/archive/2012/02/27/content-negotiation-in-asp-net-mvc4-web-api-beta-part-2.aspx
更多推荐
发布评论