我正在尝试使用在 Swashbuckle.AspNetCore (3.0.0) 的帮助下创建的 Autorest 和 Swagger 文档来生成 REST API 客户端。
生成的 swagger 文档似乎是正确的,只是操作名称不太好。
"/api/Addresses/{id}": {
"get": {
"tags": [ "Address" ],
"operationId": "ApiAddressesByIdGet",
"consumes": [],
"produces": [],
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"type": "string",
"format": "uuid"
}
],
"responses": { "200": { "description": "Success" } }
},
我在很多文章以及 SwashBuckle.AspNetCore 的官方文档中看到,我可以使用属性来装饰我的 Controller 方法,如下所示:
[HttpGet]
[Produces("application/json")]
[ProducesResponseType((int)HttpStatusCode.NotFound)]
[ProducesResponseType(typeof(List<AddressDto>), (int)HttpStatusCode.OK)]
[SwaggerOperation("GetAllAdresses")]
public async Task<IActionResult> GetAllAsync()
{
....
}
不幸的是,我遇到了一个错误:
SwaggerOperationAttribute could not be found
我验证了已安装的 NuGet 软件包,它们是:
- SwashBuckle.AspNetCore.Swagger (3.0.0)
- SwashBuckle.AspNetCore.SwaggerGen (3.0.0)
- SwashBuckle.AspNetCore.SwaggerUI (3.0.0)
如何解决这个问题?
最佳答案
我今天遇到了这个。我需要添加刚刚为 V3.0.0 添加的以下 nuget 包:
Swashbuckle.AspNetCore.Annotations
描述了重大变更 here
另请注意,您需要将以下内容添加到 Startup.cs 或 Swagger 扩展中:
AddSwaggerGen(c => { ... c.EnableAnnotations(); })
关于asp.net-core - 未找到 Swashbuckle.AspNetCore SwaggerOperation 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51974264/