-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Description
Is your feature request related to a problem? Please describe.
I'm sending a file along with some JSON data related to the file.
While this is possible today using a CustomModelBinder
https://stackoverflow.com/questions/45017720/asp-net-core-and-formdata-binding-with-file-and-json-property
var formData = new FormData();
formData.append('model', JSON.stringify({ ... }));
formData.append('file', file, file.name)
// send formData to backend
public async Task<IActionResult> Post(
[ModelBinder(BinderType = typeof(FormDataJsonBinder))] MyModel model,
IFormFile file
)
I was hoping this could be supported in a smoother way.
Describe the solution you'd like
The only extra thing that needs to happen is turning the stringified JSON back into JSON before the normal model binding can happen.
So I don't think much needs to change.
The built-in model binder should just check if the payload is FormData. Then check if any parameters are a Complex Type, if so then JSON deserialize the key matching the parameter name. When deserialized then the normal model binding happens.
public async Task<IActionResult> Post(
[FromForm] MyModel model, // <-- JSON.Deserialize(Request.Form["model"]) then ModelBind as normal
IFormFile file
)
It would be nice if FormData supported JSON. But it does not :(