将JSON发布到Web API 2时出错:此资源不支持请求实体的媒体类型“文本/纯文本" [英] Error posting JSON to Web API 2 : The request entity's media type 'text/plain' is not supported for this resource
问题描述
我上了这个课:
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; }
}
此WEB API方法:
And this WEB API method :
// POST api/fixture
public HttpResponseMessage Post(MyClass value)
{
return new HttpResponseMessage(HttpStatusCode.Created);
}
我已将Web API设置为返回JSON而不是XML,并且未对默认配置进行任何其他更改.我正在尝试使用Firefox的RESTClient扩展来测试此方法.这是我的要求:
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 :
POST localhost:XXXX/api/fixture
Content-Type: application/json
Accept: application/json
Content-Length: 60
{
value : { "Name":"Cosby","Age":"13","Description":"OK" }
}
但是我遇到此错误:
{"Message":此资源不支持请求实体的媒体类型'text/plain'.","ExceptionMessage":没有MediaTypeFormatter可用于从媒体内容中读取类型为'MyClass'的对象类型'text/plain'.," ExceptionType:" System.Net.Http.UnsupportedMediaTypeException," StackTrace:"在System.Net.Http.HttpContentExtensions.ReadAsAsync [T](HttpContent内容,类型,IEnumerable
1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable
1个格式化程序,IFormatterLogger formatterLogger,CancellationToken cancelleToken)\ r \ n位于System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage请求,类型类型,IEnumerable`1格式化程序,IFormatterLogger formatterLogger,CancellationToken cancelleToken)}
{"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, IEnumerable
1 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)"}
我不明白,因为似乎甚至没有调用该方法. 如果我进行调试,则会看到调用了构造函数,然后没有调用其他方法.没有异常.
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.
我在这个问题上已经有很多时间了.我发现,当Content-Type设置不正确时,通常会发生此问题,但由于请求未得到处理,这似乎不是我的情况.
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.
有什么主意吗?
谢谢
推荐答案
您正在正文中发送application/json
的内容类型,而不是作为标头发送.因此,您的POST请求默认为text/plain. RestClient扩展在单独的位置可以输入标题.
You were sending the content-type of application/json
in 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.
如果您对通过网络发送的内容有疑问,请查看浏览器开发人员工具中的网络"标签,或使用诸如提琴手查看网络流量.
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 Fiddler to see the network traffic.
这篇关于将JSON发布到Web API 2时出错:此资源不支持请求实体的媒体类型“文本/纯文本"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!