【发布时间】:2014-04-18 12:36:53
【问题描述】:
我正在使用 Web API 创建一个 Web 服务。我实现了一个简单的类
public class ActivityResult
{
public String code;
public int indexValue;
public int primaryCodeReference;
}
然后我在我的控制器内部实现了
[HttpPost]
public HttpResponseMessage Post(ActivityResult ar)
{
return new HttpResponseMessage(HttpStatusCode.OK);
}
但是当我调用 API 时传入 POST 文件 json:
{"code":"XXX-542","indexValue":"3","primaryCodeReference":"7"}
我收到以下错误消息:
{
"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 'ActivityResult' from content with media type 'text/plain'.",
"ExceptionType": "System.Net.Http.UnsupportedMediaTypeException",
"StackTrace": " in System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n in System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n in System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"
}
我做错了什么?
【问题讨论】:
-
您必须添加“application/json”标头才能从客户端接受有效负载。
-
我已经在我的 HTTP 请求中正确设置了标头。但是问题似乎出在服务器端:dropbox.com/s/xlidnnybs8v6d0u/Cattura.JPG
-
看起来您只是将
Accept标头设置为application/json。您还需要将Content-Type标头设置为application/json。
标签: c# asp.net .net json asp.net-web-api