c# - 在 Swagger 中生成响应错误 - .netCore Api
问题描述
为了处理所有错误,我使用 ErrorHandlingMiddleware。在任何我想要的地方抛出异常(我的实现异常),我定义类型(枚举)和错误消息。我的问题是如何在 swagger 中生成这种类型的错误,因为 swagger 只知道控制器返回类型值。并且招摇在方法中仅生成响应(200)bcs仅返回Ok();
[HttpPost("login")]
public async Task<IActionResult> Test(LoginRequest req)
{
if (user.Banned)
{
throw new BadRequestHorseedoException(ErrorCode.BANNED, "User is banned");
}
return Ok();
}
解决方案
您可以ProducesResponseType
在方法上使用属性。您的代码可能如下所示:
[HttpPost("login")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> Test(LoginRequest req)
推荐阅读
- python - 如何在 Keras 中实现层次模型?
- algorithm - 如何应用计算复杂性理论
- python - 如何创建具有重复键值的字典?
- docker - 使用 docker-compose 创建 Kafka 主题
- python - Slack App 不向频道发送消息
- angular - Angular 10 更新后:TypeError:无法分配给对象“[object Object]”的只读属性“skill_id_array”
- python - 强制 spaCy 引理为小写
- node.js - Nodejs 不识别异步函数和引用
- excel - Epplus 生成的图表在 Numbers 中丢失
- swift - 在 Swift 中调用 View 结构时添加参数