【问题标题】:CreatedAtRouteResult with swagger attribute具有招摇属性的 CreatedAtRouteResult
【发布时间】:2018-04-27 11:09:46
【问题描述】:

我有 asp.net core 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);
}

谁能解释一下如何设置这种类型的 swagger 响应属性?

【问题讨论】:

    标签: asp.net-core attributes swagger asp.net-core-webapi


    【解决方案1】:

    如果你在方法中使用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);
    }
    

    请注意,您需要为ProduceResponseType 中的每个响应代码指定返回的实际 DTO 类型。使用void 不返回正文。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-26
      • 2017-08-16
      • 2019-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多