I have previously been tasked with trying to submit json data and files to a web api shown here in this problem. The issue is I do not want to create a custom model binder for every instance this occurs so this question remains open but my current solution handling model binding from the deserialization inside the controller action is unclear to me. My code is as follows:
public async Task<IActionResult> AddDocumentReview([FromForm] string jsonData)
{
try
{
var test = JsonSerializer.Deserialize<UploadedCertificationReviewDTO>(jsonData);
return Ok(test);
}
catch(Exception ex)
{
return Ok(ex);
}
}
If I was using the model directly in the parameter, any required fields in the model would cause the request to be rejected with an error showing the data that is missing: For example:
{
"type": "https://tools.ietf.org/html/rfc9110#section-15.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"errors": {
"jsonData.state_details": [
"The StateDetails field is required."
],
"jsonData.policy_details": [
"The PolicyDetails field is required."
],
"jsonData.review_details": [
"The ReviewDetails field is required."
]
},
"traceId": "00-d3ee1dd5985a3e7724d5fdb96ff79c1c-b4aa523ff73ce8c7-00"
}
However, as you can see in my current approach I deserialize in the controller action and if this does not bind correctly it causes an exception which I return. The exception does not look like the data annotation response. So I am curious how to handle this approach and correctly send back the fields that failed to bind.