asp.net-core - 未找到 Swashbuckle.AspNetCore SwaggerOperation 属性

标签 asp.net-core swashbuckle

我正在尝试使用在 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/

相关文章:

ASP.NET Core 2 IFormCollection 返回 StringValues

c# - "VsTsc"任务无法用其输入 > 参数初始化

asp.net - 如何在 ASP.Net 的 Swashbuckle 5 中调试错误 500

c# - 将 ISchemaFilter 示例转换为 Swashbuckle 5.0

macos - 我应该将 ASP.NET Core 运行时二进制文件放在哪里?

c# - 无法从 docker 容器连接到 SQL Server

asp.net - 使用 Swashbuckle 和 ASP.NET Identity 限制对 Swagger 中某些 API Controller 的访问

c# - 最小 API - 定义 Swagger header 参数

asp.net-core - .NET & Swagger 开始出现 401 错误

asp.net-core - 获取 DevExtreme DropDownBox 选定的值