【问题标题】:Receiving multipart form-data json parameter null接收多部分表单数据 json 参数 null
【发布时间】:2020-03-20 21:18:33
【问题描述】:

我正在尝试从 Postman 接收包含 3 个参数的多部分请求:

  • int
  • 一个file
  • 一个Json

我在控制器中收到fileinteger 都很好,但是 json 的所有字段都为空。 有什么问题?

Json

    [Serializable]
    public class ProcessingRecipe
    {
        [JsonPropertyName("fileId")]
        public string FileID { get; set; }
        [JsonPropertyName("srcLang")]
        public string SrcLang { get; set; }

    }

控制器

    [HttpPost]
    [Route(Routes.Routes.File.PROCESS)]
    public async Task<ActionResult<FileProcessResponse>> ProcessFileAsync([FromForm]IFormFile uploadFile,[FromForm] ProcessingRecipe recipe,[FromForm]int aa)
    {
             //the file is ok
            // the int is 33
    }

邮递员

更新 !!!!!!

我已经按照这个post使用了,无济于事:

自定义活页夹

public class JsonModelBinder : IModelBinder {
    public Task BindModelAsync(ModelBindingContext bindingContext) {
        if (bindingContext == null) {
            throw new ArgumentNullException(nameof(bindingContext));
        }

        // Check the value sent in
        var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        if (valueProviderResult != ValueProviderResult.None) {
            bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueProviderResult);

            // Attempt to convert the input value
            var valueAsString = valueProviderResult.FirstValue;
            var result = Newtonsoft.Json.JsonConvert.DeserializeObject(valueAsString, bindingContext.ModelType);
            if (result != null) {
                bindingContext.Result = ModelBindingResult.Success(result);
                return Task.CompletedTask;
            }
        }

        return Task.CompletedTask;
    }
}

控制器动作

public async Task<ActionResult<FileProcessResponse>> ProcessFileAsync([FromForm]IFormFile uploadFile,[ModelBinder(typeof(JsonModelBinder))] ProcessingRecipe recipe)
        {
                 //the file is ok
                // the int is 33
        }

【问题讨论】:

标签: asp.net-core postman multipartform-data multipart


【解决方案1】:

这是how to upload a file and json data in postman的明确重复

在您的情况下,也许您可​​以尝试以下建议(https://stackoverflow.com/a/52748531/11226302

【讨论】:

  • 这是它对我有用的唯一方法。将 json 序列化为字符串并像input 一样将其与我的文件一起发送。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-22
  • 2016-01-26
  • 2021-03-14
  • 2020-03-12
  • 1970-01-01
  • 2016-12-17
相关资源
最近更新 更多