http://www.asp.net/web-api/overview/error-handling/web-api-global-error-handling
[HttpPost]
public async Task<IActionResult> Post(Starship starship)
{
try
{
if (starship.Classification == "Defense" &&
string.IsNullOrEmpty(starship.Description))
{
ModelState.AddModelError(nameof(starship.Description),
"For a 'Defense' ship " +
"classification, 'Description' is required.");
}
else
{
// Process the form asynchronously
// async ...
return Ok(ModelState);
}
}
catch (Exception ex)
{
logger.LogError("Validation Error: {MESSAGE}", ex.Message);
}
return BadRequest(ModelState);
}