How to wrap responses in web Api .Net Core, so that in the case without error receive data, and errors information about the error?

The crux of the matter is that now I return IActionResult in his methods
For example:
// GET /api/Person
[HttpGet]
 public async Task<ActionResult<IEnumerable<PersonDTO>>> GetAllPersons()
{
 var persons = await personService.GetAllPersons();
 logger.LogInformation("Successfully returned all persons");
 return persons;
 }

And also check the model for validity
But in the case of errors in many places code is repeated and I can only output the status code, which is not very convenient:
if (id != person.Id)
{
 return BadRequest();
}
or
if(ModelState.IsValid)
{
//something there
}

Is there any options how to do it better. It is possible to add filters or generic answers?
April 19th 20 at 12:39
0 answer

Find more questions by tags ASP.NET