I have an Api Action defined as follows:
public async Task<IActionResult> AddCountry([FromBody] CountryDto country)
{
// create the country
}
public class CountryDto
{
public Guid Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
}
When I use the payload below, it gets casted to a CountryDto type.
{
"id": "a05ddfdb-07ba-442b-9dd4-1392adf2ee5c",
"code": "CA",
"name": "Canada",
}
However, if I enter an invalid value that can't be casted like so
{
"id": 2,
"code": "CA",
"name": "Canada",
}
The countryDto parameter becomes null due to invalid casting. Is there a way for me to make the casting fail throw an exception instead of silently failing?