Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

280
Visualizações
How to have the same response format for 400 response raised from BadRequest and validations?

I have a DTO which is an input param for the API's POST endpoint.

The DTO has got data annotations and validation happens automatically. Following is an example:

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "00-f14406a8950b1005234cc79298a79586-77222ebbed97e453-00",
    "errors": {
        "Email": [
            "The Email field is not a valid e-mail address."
        ]
    }
}

However, when I want to raise 400 response from the controller, using either of the following:

return BadRequest("some message")

This returns some message

Where as this: return BadRequest(new { message = "some message"}); returns:

{
    "message": "some message"
}

How to ensure that the same format is used throughout? Is there any in-built way to standardize.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Create a base api controller as follows:

[ApiController]
public abstract class ApiControllerBase : ControllerBase
{        
    protected virtual IActionResult InvalidModelState() {
        return HttpContext.RequestServices.GetService<IOptions<ApiBehaviorOptions>>()
            .Value.InvalidModelStateResponseFactory(ControllerContext);
    }
}

And derive your api controllers from the ApiControllerBase. Whenever you want to add errors and use the same format of validation errors just add a ModelState.AddModelError("", "Your error message") then return InvalidModelState();.

over 4 years ago · Santiago Trujillo Relatório

0

As per documentation - https://docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-6.0#default-badrequest-response

The ValidationProblemDetails type:

  • Provides a machine-readable format for specifying errors in web API responses.
  • Complies with the RFC 7807 specification.

To make automatic and custom responses consistent, call the ValidationProblem method instead of BadRequest.

ValidationProblem returns a ValidationProblemDetails object as well as the automatic response.

Example:

ModelState.AddModelError("Name", "Invalid name");
return ValidationProblem(ModelState);

You can use this concept across all response codes - for example - instead of 400 (default for ValidationProblem), you can return 401 with the above code by replacing the last line with the following:

return ValidationProblem(modelStateDictionary:ModelState, statusCode:401)

If however you don't want to use this response format and want to have your own then you must disable the middleware that is responsible for automatic model validation and 400 response:

In program.cs:

builder.Services.AddControllers()
    .ConfigureApiBehaviorOptions(options =>
    {
        options.SuppressModelStateInvalidFilter=true;
    });

Now in the controller you will have to do the validations yourself (example: check for nulls) and return BadRequest(new {error = "type something here"};

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda