我没有在 swagger.json 中获得方法说明

I'm not getting a method description in swagger.json

Swagger UI 已创建,看起来很不错,只是我无法用我的方法获得描述。

c#控制器

    [Authorize]
    [ApiController]
    [ApiVersion("1.0")]
    [Route("v{version:apiVersion}/me")]
    [SwaggerTag("Me")]
    public class MeController : ControllerBase
    {

        [HttpGet(Name = "GetMe")]
        [Produces("application/json")]
        [SwaggerResponse(400, ControllerConstants.Http400Description, typeof(BadRequestMessage))]
        [SwaggerOperation("Retrieve the profile of the user", "test", OperationId = "test")]
        public async Task<IActionResult> Get()
        {
            //code
        }
    }

startup.cs

services
    .AddSwaggerGen(swagger =>
    {
        swagger.SwaggerDoc("v1", new OpenApiInfo
        {
            Title = "<title>",
            Version = "1.0",
            Contact = new OpenApiContact()
            {
                Email = "<email>",
                Name = "<name>",
            },
            Description = "<description>",

        });
        swagger.AddServer(new OpenApiServer() { Url = "http://example.com" });
    };
services
    .AddApiVersioning(options => options.ReportApiVersions = true);
services
    .AddVersionedApiExplorer(
        options =>
        {
            options.GroupNameFormat = "'v'VVV";
            options.SubstituteApiVersionInUrl = true;
        });
services
    .AddSwaggerGenNewtonsoftSupport();

swagger.json(剥离)

我希望 GetMe 操作具有 summary/description,如 https://swagger.io/docs/specification/paths-and-operations/

中所述
{
  "openapi": "3.0.1",
  "paths": {
    "/v1/me": {
      "get": {
        "tags": [
          "Me"
        ],
        "operationId": "GetMe",
        "responses": {
          "400": {
            "description": "Bad Request",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/BadRequestMessage"
                }
              }
            }
          },
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Me"
                }
              }
            }
          }
        }
      },
    },
  },
}

更新

我忽略了 EnableAnnotation 方法。 RTFM

的简单情况
swagger.EnableAnnotations();

好像定义错了OperationId。原来它已经在 HttpGet 中定义为 GetMe.

尝试引用已定义的 OperationId:

[HttpGet(Name = "GetMe")]
[SwaggerOperation("Retrieve the profile of the user", "test", OperationId = "GetMe")]
public async Task<IActionResult> Get() {..}

或仅在 SwaggerOperation:

中定义
[HttpGet]
[SwaggerOperation("Retrieve the profile of the user", "test", OperationId = "GetMe")]
public async Task<IActionResult> Get() {..}

安装并启用注释 将以下 Nuget 包安装到您的 ASP.NET 核心应用程序中。

Package Manager : Install-Package Swashbuckle.AspNetCore.Annotations
CLI : dotnet add package Swashbuckle.AspNetCore.Annotations

在 Startup.cs 的 ConfigureServices 方法中,在 Swagger 配置块中启用注释:

services.AddSwaggerGen(c =>
{
   ...

   c.EnableAnnotations();
});

摘自手册 here