将POST中的json发送到Web API服务时出错

GVi*_*i82 90 .net c# asp.net json asp.net-web-api

我正在使用Web API创建一个Web服务.我实现了一个简单的类

public class ActivityResult
{
    public String code;
    public int indexValue;
    public int primaryCodeReference;
}
Run Code Online (Sandbox Code Playgroud)

然后我在我的控制器内实现了

[HttpPost]
public HttpResponseMessage Post(ActivityResult ar)
{
    return new HttpResponseMessage(HttpStatusCode.OK);
}
Run Code Online (Sandbox Code Playgroud)

但是当我调用API传递POST文件时json:

{"code":"XXX-542","indexValue":"3","primaryCodeReference":"7"}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误消息:

{
    "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)"
}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

bea*_*ndo 182

在HTTP请求中,您需要将Content-Type设置为: Content-Type: application/json

因此,如果您使用fiddler客户端添加Content-Type: application/json到请求标头