首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在POST中将json发送到web API服务时出错

在POST中将json发送到web API服务时出错
EN

Stack Overflow用户
提问于 2014-03-14 00:06:39
回答 4查看 134.1K关注 0票数 90

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

代码语言:javascript
复制
public class ActivityResult
{
    public String code;
    public int indexValue;
    public int primaryCodeReference;
}

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

代码语言:javascript
复制
[HttpPost]
public HttpResponseMessage Post(ActivityResult ar)
{
    return new HttpResponseMessage(HttpStatusCode.OK);
}

但是当我调用API时,传入的是文件json:

代码语言:javascript
复制
{"code":"XXX-542","indexValue":"3","primaryCodeReference":"7"}

我得到以下错误信息:

代码语言:javascript
复制
{
    "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)"
}

我做错了什么?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22384354

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档