【问题标题】:Swagger UI not Displaying ModelsSwagger UI 不显示模型
【发布时间】:2021-09-27 00:21:06
【问题描述】:

在我的 api 操作上添加属性 [ApiExplorerSettings(IgnoreApi = true)] 后,Swagger UI 不显示模型。

在我的 api 操作上添加属性 [ApiExplorerSettings(IgnoreApi = true)]之前,我能够在 swagger UI 中看到模型/架构。

这就是我配置 swagger 的方式

Startup.cs 配置服务

services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version = "v1",
                    Title = "My Working API",
                    Description = "All Information related to API",
                });

                // Set the comments path for the Swagger JSON and UI.
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);
            });

配置

app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                string swaggerJsonBasePath = string.IsNullOrWhiteSpace(c.RoutePrefix) ? "." : "..";
                c.SwaggerEndpoint($"{swaggerJsonBasePath}/swagger/v1/swagger.json", "API v1");
            });
            app.UseStaticFiles();

DBContext

    /// <summary>
    /// 
    /// </summary>
    public class MyDBContext: DbContext
    {
        /// <summary>
        /// My Product DB
        /// </summary>
        public MyDBContext()
        {
        }

        /// <summary>
        /// My Product information DB
        /// </summary>
        /// <param name="options"></param>
        public MyDBContext(DbContextOptions<MyDBContext> options)
            : base(options)
        {
        }

        /// <summary>
        /// Product Model
        /// </summary>
        public virtual DbSet<Product> Product{ get; set; }

    }

我这里有什么遗漏吗?

【问题讨论】:

  • 我认为IgnoreApi 正在做应该做的事情
  • 我没有将 IgnoreApi 添加到模型中。
  • 您认为在 api 操作中使用的具有 IgnoreApi 的模型会发生什么
  • 如果是这种情况,那么应该有一种方法可以显式地显示模型。
  • 查看IDocumentFilter,您也许可以通过这种方式显式注入模型

标签: .net-core swagger swashbuckle


【解决方案1】:

要让 Swagger 显示“模式”(模型),您需要在代码中使用它。

之前: [未在 API 路由中使用的成员对象]

之后: [API 路由中使用的成员对象]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    • 2020-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    相关资源
    最近更新 更多