繁体   English   中英

我没有在 swagger.json 中得到方法描述

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

Swagger UI 已创建并且看起来很下降,但我无法使用我的方法获得描述。

c# controller

    [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
        }
    }

启动.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 操作具有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();

安装和启用注释 将以下 Nuget package 安装到您的 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();
});

取自这里的手册。

它看起来像定义了错误的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() {..}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM