我正在使用Web API创建一个网络服务。我实现了一个简单的类。
但是当我通过POST方式调用API并传递json文件时:
我收到了以下错误信息:
public class ActivityResult
{
public String code;
public int indexValue;
public int primaryCodeReference;
}
然后我在控制器内部实现了它。
[HttpPost]
public HttpResponseMessage Post(ActivityResult ar)
{
return new HttpResponseMessage(HttpStatusCode.OK);
}
但是当我通过POST方式调用API并传递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)"
}
我做错了什么?
Accept
头为application/json
。你还需要设置Content-Type
头为application/json
。 - Brian Rogers