微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我没有在 swagger.json 中得到方法描述 c# 控制器startup.csswagger.json已删除

如何解决我没有在 swagger.json 中得到方法描述 c# 控制器startup.csswagger.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 操作具有 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 包安装到您的 ASP.NET Core 应用程序中。

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

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

services.AddSwaggerGen(c =>
{
   ...

   c.EnableAnnotations();
});

取自手册here

,

看起来定义错误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",OperationId = "GetMe")]
public async Task<IActionResult> Get() {..}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。