我正在尝试从Postman
接收包含3个参数的多部分请求:
int
file
Json
我在控制器中同时收到file
和integer
罚款,但json的所有字段均为null。
有什么问题吗?
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
}
答案 0 :(得分:1)
这是how to upload a file and json data in postman
的明确副本根据您的情况,也许您可以尝试以下建议(https://stackoverflow.com/a/52748531/11226302)