I have an Attachments
json array in the request payload, and each attachment has three properties:
Name
- string and holds the name of the file to be attached.
ContentType
- System.Net.Mime.ContentType and holds the mediatype info of the file and
FileData
- string and holds base64encoded string of contents of the file/attachment.
Whenever ContentType
is null/empty
I see that the control does not even hit the controller, but the API is sending a 500 internal server error
, and the error message in the response is not helpful for the client:
Message: This property cannot be set to an empty string. (Parameter 'value')
Is there a way to let the API pass through whatever value is entered for this property(mediaType
in ContentType
) and I would like to do the validation in the API for this property and throw a 400 with a proper message. Below is the request body that is causing above error:
"Attachments": [
{
"Name": "Something.txt",
"ContentType": {
"Boundary": null,
"CharSet": null,
"MediaType": "", //this is causing the above error and I would like this one to be a bad request not a internal server error.
"Name": "SampleContentType",
"Parameters": [
{
"Key": "name",
"Value": "SampleContentType"
}
]
},
"FileData": "some base64 encoded string here"
}
]
One solution to this is to use a custom ContentType model which mimics the one being used, do the validation then map it to the original one, but I want to understand why the API is throwing 500, before it even hits the controller.
Use the ConsumesAttribute
to let the framework check it.
[Consumes("multipart/form-data")] // example
public async Task<IActionResult> MyAction { ... }
This will return a 415 by default if the Consumes
criteria is not met.