La finalidad de realizar este filtro es para organizar un poco la respuesta de un Action cuando un modelo no es valido.
En el controlador, solamente se requiere tener el decorador de [ApiController] y : ControllerBase. La accion podria ser algo como la siguiente
[HttpPost]
public async Task<ActionResult> Post([FromBody] Genero genero)
{
context.Add(genero);
await context.SaveChangesAsync();
return NoContent();
}
El API Controller valida el modelo y si el modelo no es valido, de inmediato retorna un badrequest.
Sin el filtro, el objeto que se retorna seria el siguiente
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-90c775c93bbecc4fb029a8d031227d1f-2376cbde7016724b-00",
"errors": {
"Nombre": [
"The field Nombre must be a string with a maximum length of 50.",
"La primera letra debe ser mayúscula"
]
}
}
Con el filtro seria así
[
"Nombre: The field Nombre must be a string with a maximum length of 50.",
"Nombre: La primera letra debe ser mayúscula"
]
Mas facil de manipular en el frontend.