Swashbuckle.AspNetCore 未找到 SwaggerOperation 属性

Swashbuckle.AspNetCore SwaggerOperation attribute not found

我正在尝试使用在 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 的官方文档中看到,我可以像这样使用属性来装饰我的控制器方法:

[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 包,它们是:

我该如何解决这个问题?

我运行今天跨过了这个。我需要添加刚刚为 V3.0.0 添加的以下 nuget 包:

Swashbuckle.AspNetCore.Annotations

描述了重大更改here

另请注意,您需要将以下内容添加到 Startup.cs 或您的 Swagger 扩展中:

AddSwaggerGen(c => { ... c.EnableAnnotations(); })

另一种选择是为您的 HttpGet 过滤器设置 Name 属性,如下所示:

[HttpGet(Name = "GetAllAdresses")]
[Produces("application/json")]
[ProducesResponseType((int)HttpStatusCode.NotFound)]
[ProducesResponseType(typeof(List<AddressDto>), (int)HttpStatusCode.OK)]
[SwaggerOperation("GetAllAdresses")]
public async Task<IActionResult> GetAllAsync()
{
   ....
}