Swagger UI 不显示模型

问题描述 投票:0回答:2

在我的 api 操作上添加属性

[ApiExplorerSettings(IgnoreApi = true)]
后,Swagger UI 不显示模型。

在我的 api 操作上添加属性

[ApiExplorerSettings(IgnoreApi = true)]
之前,我能够在 swagger UI 中看到模型/架构。

这就是我配置 swagger 的方式

启动.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; }

    }

我在这里遗漏了什么吗?

.net-core swagger swashbuckle
2个回答
3
投票

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

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

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


0
投票

我很愚蠢,没有将 getter 和 setter 添加到模型类中。因此 Swagger 无法访问这些属性。您或其他 Google 员工可能也有同样的情况。

© www.soinside.com 2019 - 2024. All rights reserved.