C# ASP.NET Web API - 没有“MediaTypeFormatter”可用于读取“Int32”类型的对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9979123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET Web API - No 'MediaTypeFormatter' is available to read an object of type 'Int32'
提问by Roberto Bonini
I'm not entirely sure whats happened here. I may have messed things up somewhere, but I don't know what.
我不完全确定这里发生了什么。我可能在某处搞砸了,但我不知道是什么。
My API controller method looks like this:
我的 API 控制器方法如下所示:
public HttpResponseMessage<string> Put(int id)
I've tried a string as well with the same error.
我也试过一个字符串,也有同样的错误。
Any ideas?
有任何想法吗?
Thanks.
谢谢。
Edit:To be clear - the id is the route parameter. The body of the request is JSON. If I remove the route parameter,the method functions as normal.
编辑:要清楚 - id 是路由参数。请求的正文是 JSON。如果我删除路由参数,该方法将正常运行。
采纳答案by tpeczek
Please read following blog post:
请阅读以下博文:
This describes most typical issues with using simple parameters. Without knowing any more details about how your request looks it is not possible to determine which one you have hit.
这描述了使用简单参数的最典型问题。如果不了解有关您的请求外观的更多详细信息,则无法确定您击中的是哪一个。
UPDATE
更新
There is one more known bug considering route values. In case when not one of built in formatters is used for POST/PUT/PATCH request, the route values parameters are not being bind. To work around this the best solution is to write ActionFilterAttribute as described below:
考虑到路由值,还有一个已知的错误。如果没有任何内置格式化程序用于 POST/PUT/PATCH 请求,则不会绑定路由值参数。要解决此问题,最好的解决方案是编写 ActionFilterAttribute,如下所述:
回答by Despertar
Surprisingly, int and string do not have a MediaTypeFormatter by default, so it doesn't know how to handle those types.
令人惊讶的是,默认情况下 int 和 string 没有 MediaTypeFormatter,因此它不知道如何处理这些类型。
The only types it knows how to handle out of the box are JSON, XML, and form url-encoded data. This quote is from the official asp.net website, http://www.asp.net/web-api/overview/formats-and-model-binding/media-formatters
它知道如何开箱即用的唯一类型是 JSON、XML 和表单 url 编码数据。此引述来自asp.net 官方网站,http://www.asp.net/web-api/overview/formats-and-model-binding/media-formatters
In Web API, the media type determines how Web API serializes and deserializes the HTTP message body. There is built-in support for XML, JSON, and form-urlencoded data, and you can support additional media types by writing a media formatter.
在 Web API 中,媒体类型决定了 Web API 如何序列化和反序列化 HTTP 消息体。内置对 XML、JSON 和表单 urlencoded 数据的支持,您可以通过编写媒体格式化程序来支持其他媒体类型。
Now you 'can' write your own MediaTypeFormatter (the link I provided will show you how), but since the asp.net web api is still in beta I have had a lot of trouble with it using custom formatters for simple types like strings. I found it is much easier just to wrap whatever value you would like to PUT in xml / json and it will automatically get deserialized. See my post here for more info on that, When HTTP-POST has body, url parameter is null
现在您“可以”编写自己的 MediaTypeFormatter(我提供的链接将向您展示如何编写),但是由于 asp.net web api 仍处于测试阶段,因此我在使用自定义格式化程序来处理字符串等简单类型时遇到了很多麻烦。我发现将任何您想放置在 xml / json 中的值包装起来要容易得多,并且它会自动反序列化。有关更多信息,请参阅我在此处的帖子,当 HTTP-POST 具有正文时,url 参数为空
For your specific example your PUT body would look like,
对于您的具体示例,您的 PUT 主体看起来像,
<message>
<id>6</id>
</message>
Then be sure to set the content-type of your http request to text/xml (or application/json if you choose to use that). And it should serialize just fine into the variable.
然后确保将您的 http 请求的内容类型设置为 text/xml(或 application/json,如果您选择使用它)。它应该很好地序列化到变量中。
回答by Mike Wasson
Are you placing the int value in the body of the request message? If so, what is the format?
您是否将 int 值放在请求消息的正文中?如果有,格式是什么?
If the content type is text, Web API won't know what to do with it, because Web API does not provide a media formatter for text. You can write a custom formatter, as Despertar noted.
如果内容类型是文本,Web API 将不知道如何处理它,因为 Web API 不提供文本的媒体格式化程序。正如 Despertar 所指出的,您可以编写自定义格式化程序。
You can also do something like this:
你也可以做这样的事情:
public void Put()
{
var s = Request.Content.ReadAsStringAsync().Result;
}
回答by Ken
I posted a DELETE with a json body and received this. Posting with parameters resolved the problem.
我发布了一个带有 json 正文的 DELETE 并收到了这个。使用参数发布解决了问题。
回答by Prisoner ZERO
On a side note...I have seen it throw this error when the service started requiring 'HTTPS'.
附带说明......我看到它在服务开始需要“HTTPS”时抛出此错误。

