[英]Hide swagger bad response example model in net core 2.2
I upgrade my.netcore 2.1 project to 2.2 and i have a problem with my swagger page.我将 my.netcore 2.1 项目升级到 2.2,我的 swagger 页面出现问题。
Previously in swagger bad response it only show "Bad Request" without the model.以前在 swagger 错误响应中它只显示“错误请求”而没有 model。
But after i upgraded to.net core 2.2 there is a model shown in the bad request example.The image is below.但是在我升级到 .net core 2.2 之后,错误请求示例中显示了一个 model。图像如下所示。
How do i hide it so that it just show the "Bad request".我如何隐藏它以便它只显示“错误请求”。
I already tested using CustomApiConvention but no success so far.我已经使用 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;
}
How do i hide it so that it just show the "Bad request"?我如何隐藏它以便它只显示“错误请求”?
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. 这将禁用ProblemDetails响应。
https://docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-2.2#problem-details-for-error-status-codes https://docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-2.2#problem-details-for-error-status-codes
The solution when using .NET 5.0 is to update the Startup.cs
file and add this part to the ConfigureServices
method使用.NET 5.0时的解决方法是更新Startup.cs
文件,将这部分添加到ConfigureServices
方法中
services.AddControllers()
.ConfigureApiBehaviorOptions(options =>
{
options.SuppressMapClientErrors = true;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.