我试图在startup.cs中配置swagger。如果我给它所需的值“硬编码”,如下面的in ASP.NET Core : 2.2
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("V1", new Info { Title = "MY.API", Version ="v1" });
});
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
// specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "MY.API V1");
});
但如果我从asp.net核心提供的“IConfiguration”对象设置所有上面的硬编码值,那么它就会停止工作,
services.AddSwaggerGen(c =>
{
c.SwaggerDoc(this.Configuration.GetSection("AppSetting")
["APIName"], new Info { Title = "My.API", Version = "v1" });
});
AS:总是10 Pro
这应该是评论,但我没有足够的业力。
首先请告诉我们您的appsettings.json。第二次检查这一行是否实际返回正确的值而不是null或空字符串(只需将其放在local var中):
this.Configuration.GetSection("AppSetting")["APIName"]
编辑:似乎名称字段区分大小写。确保在UseSwaggerUI中也使用config中的值:
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint($"/swagger/{this.Configuration.GetSection("AppSetting")["APIName"]}/swagger.json", "MY.API V1");
});