接收多部分表单数据json参数null

问题描述 投票:0回答:1

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

  • [int
  • A file
  • A Json

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

[Json

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

    }

Controller

    [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
    }

邮递员

enter image description here

更新 !!!!!

我根据此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个回答
1
投票
© www.soinside.com 2019 - 2024. All rights reserved.