【问题标题】:Passing in two different JSON objects FromBody to a controller将两个不同的 JSON 对象 FromBody 传递给控制器
【发布时间】:2019-08-20 20:26:22
【问题描述】:

我有两个 JSON 对象需要传递到我的 MVC .net 核心控制器的端点。这是 API 端点的签名:

[HttpPost]
    public async Task<ActionResult<FileTransLationAccepted>> GetFileText([FromBody]FileInformation fileInfo, [FromBody]FileUpload busMessage)
    {

        //Call the processing file here.  Get the ID from the JSON passed in for file download
        return await _fileDownload.DownloadFile(fileInfo, busMessage);

    }

这是两个 JSON 对象(我必须传入的模型):

 public class FileUpload
{
    public Metadata metadata { get; set; }
}

public class Metadata
{
    public string[] caseNumbers { get; set; }
    public string[] tagValueIds { get; set; }
    public Textualtag[] textualTags { get; set; }
}

public class Textualtag
{
    public string tagCategoryId { get; set; }
    public string[] textValues { get; set; }
}

这是另一个:

 public class FileInformation
{
    public string CompanyId{ get; set; }
    public string FileId { get; set; }
    public string FileName { get; set; }
    public string FileURL { get; set; }

}

是否可以在正文中传入两个 JSON 文件而不必进行两次调用?我尝试删除 [FromBody] 属性,但不断收到 MVC 错误:

[method] 有多个参数被指定或推断为从请求正文绑定。每个动作只能绑定一个参数。

那么我需要将两个 JSON 对象组合成一个更大的 JSON,还是有办法在一次调用中传入这两个对象?

谢谢。

【问题讨论】:

    标签: c# api asp.net-core


    【解决方案1】:

    我没有用谷歌搜索确定,但我有 90% 的把握你只会得到一个 FromBody。最好将它们结合起来:

    public class Combined
    {
        public FileInformation fileInformation{ get; set; }
        public FileUpload fileUpload{ get; set; }
    }
    

    事后诸葛亮:找到这个... Multiple types [FromBody] on same method .net core web api

    【讨论】:

      猜你喜欢
      • 2017-01-11
      • 1970-01-01
      • 1970-01-01
      • 2012-01-16
      • 2016-02-28
      • 2011-09-05
      • 1970-01-01
      • 1970-01-01
      • 2016-01-04
      相关资源
      最近更新 更多