gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:28:35 26 4
gpt4 key购买 nike

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

以前在 swagger 错误响应中它只显示“Bad Request”而没有模型。

但在我升级到 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;
}

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

enter image description here

最佳答案

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

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

关于c# - 在 net core 2.2 中隐藏 swagger 不良响应示例模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56341749/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com