【发布时间】: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