在 net core 2.2 中隐藏 swagger 不良响应示例模型

Hide swagger bad response example model in net core 2.2

我将我的 netcore 2.1 项目升级到 2.2,但我的 swagger 页面有问题。

以前在 swagger 糟糕的响应中它只显示 "Bad Request" 而没有模型。

但在我升级到 net core 2.2 后,错误请求中显示了一个模型 example.The 图片如下。

如何隐藏它以便它只显示 "Bad request"。

我已经使用 CustomApiConvention 进行了测试,但到目前为止没有成功。

    [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;
    }

如何隐藏它以便只显示 "Bad request"?

对于遇到同样问题的任何人。只需将其添加到您的代码中即可。

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

这将禁用 ProblemDetails 响应。

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

使用.NET 5.0 时的解决方案是更新Startup.cs 文件并将此部分添加到ConfigureServices 方法

services.AddControllers()
    .ConfigureApiBehaviorOptions(options =>
    {
        options.SuppressMapClientErrors = true;
    });