【问题标题】:How to get complex data from [FromBody] attribute in WebAPI如何从 WebAPI 中的 [FromBody] 属性获取复杂数据
【发布时间】:2015-11-06 12:48:03
【问题描述】:

当我尝试发送请求“http://localhost:1234/api/case/create?signature=123456” 来自 Postman (Google Extension) 在正文请求中使用“form-data”,我收到错误:

"Message": "该资源不支持请求实体的媒体类型'multipart/form-data'。", "ExceptionMessage": "没有 MediaTypeFormatter 可用于从媒体类型为 'multipart/form-data' 的内容中读取类型为 'Case' 的对象。", “异常类型”:“System.Net.Http.UnsupportedMediaTypeException”。

我的行动:

    [Route("create")]
    public object Create([FromBody]Case newCase, string signature)
    {
        var member = _memberService.GetUserByToken(signature);
        if (member != null)
        {
            var caseId = _caseService.Add(newCase, member);

            return Ok(new { caseId });
        }

        return NotFound();
    }

【问题讨论】:

  • 显示您的请求内容类型

标签: c# asp.net asp.net-web-api


【解决方案1】:

你应该在 Postman 中添加 header Content-Type: application/json

【讨论】:

  • 我在 Postman 中添加了标题“Content-Type: application/json”,但后来我得到了 newCase = null。
  • 尝试使用原始数据进行 POST。看这里stackoverflow.com/questions/19037636/…
  • 是的,它适用于原始数据和 Content-Type: application/json,谢谢。但是表单数据和原始数据有什么区别?我要编写控制台应用程序来测试我的 api。
  • 这里getpostman.com/docs/requests你可以在邮递员中找到关于请求的描述。简而言之:您的 Web api 操作需要原始 json 数据,然后将其序列化为对象。当您在邮递员中使用表单数据时,请求就像字典(键 => 值)。
猜你喜欢
  • 2018-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-15
  • 1970-01-01
  • 1970-01-01
  • 2019-12-03
  • 1970-01-01
相关资源
最近更新 更多