yoz*_*epi 5 c# swagger asp.net-web-api2 swashbuckle
我正在编写一个演示购物车 API,我遇到了一种情况,我可以出于多种原因返回 NotFound(未找到购物车项目或未找到购物车本身)。我想为这两种情况返回 Swagger 描述。我试过这个,但它不起作用。
[SwaggerResponse(HttpStatusCode.NotFound, Type = typeof(CartNotFoundResponse), Description = "Cart not found (code=1)")]
[SwaggerResponse(HttpStatusCode.NotFound, Type = typeof(ItemNotFoundResponse), Description = "Item not found (code=104)")]
[SwaggerResponse(HttpStatusCode.NoContent)]
public async Task<IHttpActionResult> DeleteItemWithSKUAsync(Guid cartId, string sku)
{
}
Run Code Online (Sandbox Code Playgroud)
有什么建议?
不幸的是,当前的实现是不可能的: https://github.com/domaindrivendev/Swashbuckle/blob/master/Swashbuckle.Core/Swagger/SwaggerDocument.cs#L29
正如您所看到的,响应是一个字典,键是 StatusCode。
我的建议:使用更通用的类来涵盖这两种情况(CartNotFound 和 ItemNotFound)