12

I upgrade my netcore 2.1 project to 2.2 and i have a problem with my swagger page.

Previously in swagger bad response it only show "Bad Request" without the model.

But after i upgraded to net core 2.2 there is a model shown in the bad request example.The image is below.

How do i hide it so that it just show the "Bad request".

I already tested using CustomApiConvention but no success so far.

    [HttpGet("{id}")]
    [ProducesResponseType(StatusCodes.Status204NoContent)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [ProducesDefaultResponseType]
    public async Task<ActionResult<Employee>> GetEmployee(int id)
    {
        var employee = await _context.Employee.FindAsync(id);

        if (employee == null)
        {
            return NotFound();
        }

        return employee;
    }

How do i hide it so that it just show the "Bad request"?

enter image description here

stuartd
  • 70,509
  • 14
  • 132
  • 163
capudang
  • 403
  • 4
  • 10
  • Can't test but I assume removing `ProducesDefaultResponseType` would do so. For the 2xx responsetypes you would then have to add the appropriate `typeof` in the `ProducesResponseType` attribute. – NotFound May 28 '19 at 12:21
  • Nope, it is still the same. – capudang May 28 '19 at 12:38

2 Answers2

18

To anyone who got the same problem. Just add this to your code.

services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.ConfigureApiBehaviorOptions(options =>
{
    options.SuppressMapClientErrors = true;
});

This will disable ProblemDetails response.

https://learn.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-2.2#problem-details-for-error-status-codes

capudang
  • 403
  • 4
  • 10
  • 2
    The `.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)` gave a warning (in my ASP.NET Core 3.0 project) but instead of changing to `.Version_3_0` just leaving it out completely works too. – Glorfindel Dec 19 '19 at 13:31
  • 3
    Brilliant, I've been searching this for days! I'm using `net5.0` and this worked for me: `services.AddControllers(x => x.Conventions.Add(new ApiExplorerVersionConvention())).ConfigureApiBehaviorOptions(x => { x.SuppressMapClientErrors = true; });` – user1574598 May 25 '21 at 10:39
2

The solution when using .NET 5.0 is to update the Startup.cs file and add this part to the ConfigureServices method

services.AddControllers()
    .ConfigureApiBehaviorOptions(options =>
    {
        options.SuppressMapClientErrors = true;
    });
user1242967
  • 1,220
  • 3
  • 18
  • 30