【发布时间】:2014-11-14 03:37:03
【问题描述】:
我有这门课:
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 方法:
// POST api/fixture
public HttpResponseMessage Post(MyClass value)
{
return new HttpResponseMessage(HttpStatusCode.Created);
}
我已将 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" }
}
但是我收到了这个错误:
{"Message":"此资源不支持请求实体的媒体类型 'text/plain'。","ExceptionMessage":"没有 MediaTypeFormatter 可用于从具有媒体的内容中读取类型为 'MyClass' 的对象在 System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent 内容,类型类型,IEnumerable@ 987654324@1 格式化程序,IFormatterLogger formatterLogger,CancellationToken cancelToken)\r\n 在 System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage 请求,类型类型,IEnumerable`1 格式化程序,IFormatterLogger formatterLogger,CancellationToken cancelToken)"}
编辑:
我不明白,因为似乎甚至没有调用该方法。 如果我调试,我看到构造函数被调用,然后没有调用其他方法。不会引发异常。
我在这个问题上已经有很多时间了。我发现这个问题通常发生在 Content-Type 设置不正确的情况下,但似乎不是我的情况,因为请求甚至没有被处理。
有什么想法吗?
谢谢
【问题讨论】:
-
您不需要在问题中混淆您的端口号。我们不知道
localhost在哪里。无论如何,您确定您的异常发生在该 POST 消息中吗?该异常听起来像是您发布的内容类型为text/plain而不是application/json。
标签: json asp.net-web-api asp.net-web-api2