将 JSON 发布到 Web API 2 时出错:此资源不支持请求实体的媒体类型“text/plain”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25935650/
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
Error posting JSON to Web API 2 : The request entity's media type 'text/plain' is not supported for this resource
提问by tobiak777
I have this class :
我有这门课:
public class MyClass
{
public MyClass() { Secret = "Don't tell me"; }
public string Name { get; set; }
public int Age { get; set; }
public string Description { get; set; }
private string Secret { get; set; }
}
And this WEB API method :
而这个 WEB API 方法:
// POST api/fixture
public HttpResponseMessage Post(MyClass value)
{
return new HttpResponseMessage(HttpStatusCode.Created);
}
I've set Web API to return JSON instead of XML and I haven't done any other change to the default config. I'm trying to test this method with the RESTClient extension of Firefox. Here is my request :
我已将 Web API 设置为返回 JSON 而不是 XML,并且我没有对默认配置进行任何其他更改。我正在尝试使用 Firefox 的 RESTClient 扩展来测试此方法。这是我的要求:
POST localhost:XXXX/api/fixture
Content-Type: application/json
Accept: application/json
Content-Length: 60
{
value : { "Name":"Cosby","Age":"13","Description":"OK" }
}
However I'm getting this error :
但是我收到此错误:
{"Message":"The request entity's media type 'text/plain' is not supported for this resource.","ExceptionMessage":"No MediaTypeFormatter is available to read an object of type 'MyClass' from content with media type 'text/plain'.","ExceptionType":"System.Net.Http.UnsupportedMediaTypeException","StackTrace":" at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable
1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"}
{"Message":"此资源不支持请求实体的媒体类型 'text/plain'。","ExceptionMessage":"没有 MediaTypeFormatter 可用于从媒体类型为 'text' 的内容中读取类型为 'MyClass' 的对象/plain'.","ExceptionType":"System.Net.Http.UnsupportedMediaTypeException","StackTrace":" at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable
1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable1 formatters, IFormatterLogger formatterLogger、CancellationToken 取消令牌)\r\n 在 System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage 请求、类型类型、IEnumerable`1 格式化程序、IFormatterLogger formatterLogger、CancellationToken 取消令牌)"}
Edit:
编辑:
I don't understand because it seems that the method is not even called. If I debug, I see that the constructor is called and then no other method is called. No exception is raised.
我不明白,因为似乎甚至没有调用该方法。如果我调试,我会看到构造函数被调用,然后没有其他方法被调用。没有异常被提出。
I've been on this issue for a lot of time now. I've found that this problem usually happens when Content-Type is not set properly but it doesn't seem to be my case since the request is not even processed.
我已经在这个问题上讨论了很长时间了。我发现这个问题通常发生在 Content-Type 设置不正确时,但似乎不是我的情况,因为请求甚至没有被处理。
Any idea ?
任何的想法 ?
Thanks
谢谢
采纳答案by mason
You were sending the content-type of application/jsonin the body, rather than as a header. So your POST request was defaulting to text/plain. The RestClient extension has a separate place to enter the header.
您application/json在正文中发送了内容类型,而不是作为标题发送。所以你的 POST 请求默认为 text/plain。RestClient 扩展有一个单独的地方来输入标题。
If you ever have a question about what's being sent over the wire, check the Network tab in your browser's developer tools, or use a tool such as Fiddlerto see the network traffic.
如果您对通过网络发送的内容有任何疑问,请查看浏览器开发人员工具中的“网络”选项卡,或使用Fiddler等工具查看网络流量。
回答by Azlan Jamal
回答by Baqer Naqvi
Probably you need to add following contents in your HTTPrequest header:
可能您需要在HTTP请求标头中添加以下内容:
Content-Type: application/json
回答by Json
Use Postman to test your Web API, configure it to have header: content-type: application/json Since that is a POST request, you have to put the raw json data.
使用 Postman 测试您的 Web API,将其配置为具有 header: content-type: application/json 由于这是一个 POST 请求,您必须放置原始 json 数据。
回答by LastTribunal
I got this problem when I had a controller that had two different models in the params. The Post method was using a model that was not specified in the builder.EntitySetThe way I solved this is to create a separate read and write controllers. This is along the lines of CQRS. Although the API specs become more messy.. thank god for Swagger!
当我的控制器在 params 中有两个不同的模型时,我遇到了这个问题。Post 方法使用了一个未在builder.EntitySet 中指定的模型, 我解决这个问题的方法是创建一个单独的读写控制器。这是沿着 CQRS 的路线。虽然 API 规范变得更加混乱..感谢上帝的 Swagger!


