【问题标题】:REST API model bind for complex model + collection + IFormFile用于复杂模型 + 集合 + IFormFile 的 REST API 模型绑定
【发布时间】:2020-01-25 10:10:16
【问题描述】:

我有一个动作应该作为参数复杂模型。它应包含以下应模型绑定的属性:

  1. IFormFile(绑定已完成)
  2. 字符串属性(绑定已完成)
  3. 字典

我在绑定到 UploadHeaders 类的元数据集合时遇到问题。我尝试过的所有方法都不起作用。我尝试了 FormUrlEncodedContent 和 StringContent (作为 JSON)。有人可以指出我正确的方向,除了 IFormFile 和字符串属性之外,如何绑定集合? FileMetadata 对象是只有 Key 和 Value 属性的类的集合。两者都是公开的。

服务器端:

动作模型:

public class UploadHeaders
{
    public IFormFile File { get; set; }

    public IList<FileMetadata> Metadata { get; set; }

    public string Author { get; set; }

    public int AuthorId { get; set; }

    public UploadHeaders()
    {
        this.Metadata = new List<FileMetadata>();
    }
}

行动:

public async Task<ActionResult> Upload([FromForm] UploadHeaders uploadHeaders)
{ 
...
}

调用服务器端上传操作的客户端方法:

static UploadResponse Upload(string fileToUpload, string fileName)
    {
        var multipartFormDataContent = new MultipartFormDataContent();
        var fileContent = new ByteArrayContent(File.ReadAllBytes(fileToUpload));
        multipartFormDataContent.Add(fileContent, "file", fileName);
        multipartFormDataContent.Add(new StringContent("Author"), "Author");
        multipartFormDataContent.Add(new StringContent("13"), "AuthorId");

        var metadataStringContent = new FormUrlEncodedContent(
            new []
            {
                new KeyValuePair<string, string>("Metadata[0].Key", "key1"),
                new KeyValuePair<string, string>("Metadata[0].Value", "value1")
            }
        );

        multipartFormDataContent.Add(metadataStringContent, "meta");

        var response = httpClient.PostAsync(httpClient.BaseAddress + "Upload", multipartFormDataContent).Result;

        string stringResult = response.Content.ReadAsStringAsync().Result;
        UploadResponse uploadResponse = JsonConvert.DeserializeObject<UploadResponse>(stringResult);
        Console.WriteLine("{0} to {1}, result {2}", fileToUpload, fileName, uploadResponse.Id);
        return uploadResponse;
    }

【问题讨论】:

    标签: c# .net rest model-binding


    【解决方案1】:

    为了绑定集合,您还应该将其值作为StringContent 传递

    var multipartFormDataContent = new MultipartFormDataContent();
    var fileContent = new ByteArrayContent(File.ReadAllBytes(fileToUpload));
    multipartFormDataContent.Add(fileContent, "file", fileName);
    multipartFormDataContent.Add(new StringContent("Author"), "Author");
    multipartFormDataContent.Add(new StringContent("13"), "AuthorId");
    
    // parameter name should follow this template
    // {collection_name}[{index}].{property}
    multipartFormDataContent.Add(new StringContent("key1"), "Metadata[0].Key");
    multipartFormDataContent.Add(new StringContent("value1"), "Metadata[0].Value");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-05
      • 2015-08-30
      • 2017-03-26
      • 1970-01-01
      • 2014-12-29
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      相关资源
      最近更新 更多