【问题标题】:SwashBuggler.swaggerDocument fails to created documentSwashBuggler.swaggerDocument 未能创建文档
【发布时间】:2019-12-30 11:55:05
【问题描述】:

项目结构:

  1. 控制台应用程序 - 启动 WebAPI 启动
  2. API 项目(DLL) - 仅配置(使用 (AssemblyPart) 的容器和授权以及控制器类初始化)
  3. ControllerClass(DLL) - 具有 API 方法

SwaggerDocumentaion 配置, 配置服务类代码:

    services.AddSwaggerGen(options =>
        {
            options.DescribeAllEnumsAsStrings();
            options.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info
            {
                Title = "TestAPI",
                Version = "v1",
                Description = "TestApplication API for modular monolith .NET application.",
            });

            var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
            var commentsFileName = "Test.Modules.UserAccess.Facade.XML";//Assembly.GetExecutingAssembly().GetName().Name + ".XML";
            var commentsFile = Path.Combine(baseDirectory, commentsFileName);
            options.IncludeXmlComments(commentsFile);

            var securityToken = new Dictionary<string, IEnumerable<string>>
            {
                {"Bearer", new string[] { }},
            };

            options.AddSecurityDefinition("Bearer", new ApiKeyScheme
            {
                Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
                Name = "Authorization",
                In = "header",
                Type = "apiKey"
            });
            options.AddSecurityRequirement(securityToken);
        });

配置类:

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider 
  serviceProvider)
    {
        app.UseSwagger();
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "TestAPI");
            c.RoutePrefix = string.Empty;

        });
    }

我已生成 XML 文件并复制到输出目录。

问题:

我收到错误“无法加载 API 定义”。帮我解决这个问题。

【问题讨论】:

  • 发现问题。 Swashbuckle 失败抛出 NotSupportedException 异常。修复异常。正确区分get方法。例如[HTTPGet("{id}")] 。生成该文档后。

标签: c# swagger asp.net-core-2.1 swashbuckle


【解决方案1】:

你能仔细检查控制器方法吗? 例如,Swagger 需要显式的 [HttpPost][HttpGet] 如果您有Get(int id)Post(..),这将返回Failed to Load API Definition

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    • 2019-09-09
    • 2022-08-14
    • 2017-01-28
    • 1970-01-01
    • 2021-05-19
    相关资源
    最近更新 更多