我正在使用Web API创建Web服务。我实现了一个简单的类将POST中的json发送到Web API服务时出错
public class ActivityResult
{
public String code;
public int indexValue;
public int primaryCodeReference;
}
然后,我有我的控制器
[HttpPost]
public HttpResponseMessage Post(ActivityResult ar)
{
return new HttpResponseMessage(HttpStatusCode.OK);
}
内部实现但当我称之为POST传递文件JSON的API:
{"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请求中正确设置了标头。然而,这个问题似乎是服务器端:https://www.dropbox.com/s/xlidnnybs8v6d0u/Cattura.JPG – GVillani82
它看起来像只设置'Accept'头到'application/json'。您还需要将“Content-Type”头设置为“application/json”。 –