在net core 2.2中隐藏swagger错误响应示例模型

2023-12-27

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

以前在 swagger 错误响应中,它只显示“错误请求”,没有模型。

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

我如何隐藏它以便它只显示“错误请求”。

我已经使用 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;
    }

我如何隐藏它以便它只显示“错误请求”?


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

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

这将禁用 ProblemDetails 响应。

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在net core 2.2中隐藏swagger错误响应示例模型 的相关文章

随机推荐