找不到Swashbuckle.AspNetCore SwaggerOperation属性

Séb*_*jci 16 swashbuckle asp.net-core

我正在尝试使用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" } }
      },
Run Code Online (Sandbox Code Playgroud)

我在许多文章和SwashBuckle.AspNetCore的官方文档中看到我可以使用属性来装饰我的控制器方法,如下所示:

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

不幸的是,我收到了一个错误:无法找到SwaggerOperationAttribute!

我验证了安装的nuget包,它们是:

  • SwashBuckle.AspNetCore.Swagger(3.0.0)
  • SwashBuckle.AspNetCore.SwaggerGen(3.0.0)
  • SwashBuckle.AspNetCore.SwaggerUI(3.0.0)

有人可以帮帮我吗?请

Sco*_*ttG 21

我今天碰到了这个.我需要添加以下为V3.0.0添加的nuget包:

Swashbuckle.AspNetCore.Annotations

这里描述重大变化

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

AddSwaggerGen(c => { ... c.EnableAnnotations(); })
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,帮助了我.也很好评论,起初它对我不起作用我必须在Startup.cs services.AddSwaggerGen中启用Annotations(c => {... c.EnableAnnotations();}); 更多信息:https://github.com/domaindrivendev/Swashbuckle.AspNetCore#swashbuckleaspnetcoreannotations (2认同)