asp.net-core - 具有招摇属性的 CreatedAtRouteResult
问题描述
我有 asp.net 核心 web api。我的一个控制器返回 CreatedAtRouteResult。如何为此方法添加 swagger 属性。
[HttpPost]
[SwaggerResponse(400, typeof(NotFoundResult))]
[SwaggerResponse(201, typeof(CreatedAtRouteResult))]
public async Task<IActionResult> Create([FromBody] SubscriptionDTO dto)
{
var issuedTo = (await _tokenService.Get()).IssuedTo;
SubscriptionDresult = await _subscriptionService.CreateAsync(dto, issuedTo.Id);
return result == null ? (IActionResult)NotFound(): CreatedAtRoute(new {id = result.Id}, result);
}
有人可以解释如何设置这种类型的招摇响应属性吗?
解决方案
如果您ProduceResponseType
在方法上使用属性,swagger 会自动将其拾取并将其添加到 swagger.json 文档中。
[HttpPost]
[ProducesResponseType(typeof(SubscriptionDTO), (int)HttpStatusCode.Created)]
[ProducesResponseType(typeof(void), (int)HttpStatusCode.NotFound)]
public async Task<IActionResult> Create([FromBody] SubscriptionDTO dto)
{
var issuedTo = (await _tokenService.Get()).IssuedTo;
SubscriptionDTO result = await _subscriptionService.CreateAsync(dto, issuedTo.Id);
return result == null ? (IActionResult)NotFound(): CreatedAtRoute(new {id = result.Id}, result);
}
请注意,您需要指定为每个响应代码返回的实际 DTO 类型
ProduceResponseType
。用于void
无返回体。
推荐阅读
- android - Windows 10 上 Android Studio AVD 的解决方法
- php - 在 PHP 中解析 Google 服务对象
- python - Flask-RESTplus 中的响应类
- r - R - 为多个变量查找一周中同一天的变化并聚合
- python-3.x - 如何在 Python 中有效地追加运行总和?
- node.js - 除非从新的浏览器选项卡加载页面,否则 Node Express 应用程序会冻结
- javascript - v-显示嵌套数组项是否匹配过滤数组
- java - 我应该把 public static void main(String[] args) 放在哪里?
- javascript - 数学程序中的 JavaScript 输出
- c++ - 错误 E0289:没有构造函数“Movie::Movie”的实例与基本程序 (C++) 的参数列表匹配