当我从 'Development' 的值更改 ASPNETCORE_ENVIRONMENT 的变量时,Swagger 页面不起作用
When I change the variable of ASPNETCORE_ENVIRONMENT from the value of 'Development', the Swagger page doesn't work
我有一个基于 dot net 6 的 Web API。
此 Web API 在 Azure 应用服务上运行。 Azure 应用服务在 Linux.
上运行
我正在使用 Open API (Swagger) 进行测试和记录。
我创建了 2 个 appsettings 文件。 (发展和阶段。)
我正在将 ASPNETCORE_ENVIRONMENT 变量添加到 Azure 应用服务的配置中,如下所示。函数适用于 ASPNETCORE_ENVIRONMENT(开发和阶段)的两个变量。
当我将 'Stage' 值设置为 ASPNETCORE_ENVIRONMENT 时,Swagger 页面无法正常工作。它给出了 404 Not Found 错误。但它适用于开发。
这是我的 swagger 配置启动代码;
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options => options.AddPolicy("AllowAnyOrigin", builder => { builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader(); }));
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "AllStore.Api", Version = "v1" });
var securitySchema = new OpenApiSecurityScheme
{
Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Scheme = "bearer",
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
};
c.AddSecurityDefinition("Bearer", securitySchema);
var securityRequirement = new OpenApiSecurityRequirement
{
{ securitySchema, new[] { "Bearer" } }
};
c.AddSecurityRequirement(securityRequirement);
});
}
我真的不明白这是什么问题?谁能帮帮我?
更改了 startup.cs 中的 Configure 方法,如下所示
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "AllStore.Api v1"));
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
而不是
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "AllStore.Api v1"));
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
我有一个基于 dot net 6 的 Web API。 此 Web API 在 Azure 应用服务上运行。 Azure 应用服务在 Linux.
上运行我正在使用 Open API (Swagger) 进行测试和记录。 我创建了 2 个 appsettings 文件。 (发展和阶段。)
我正在将 ASPNETCORE_ENVIRONMENT 变量添加到 Azure 应用服务的配置中,如下所示。函数适用于 ASPNETCORE_ENVIRONMENT(开发和阶段)的两个变量。
当我将 'Stage' 值设置为 ASPNETCORE_ENVIRONMENT 时,Swagger 页面无法正常工作。它给出了 404 Not Found 错误。但它适用于开发。
这是我的 swagger 配置启动代码;
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options => options.AddPolicy("AllowAnyOrigin", builder => { builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader(); }));
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "AllStore.Api", Version = "v1" });
var securitySchema = new OpenApiSecurityScheme
{
Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Scheme = "bearer",
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
};
c.AddSecurityDefinition("Bearer", securitySchema);
var securityRequirement = new OpenApiSecurityRequirement
{
{ securitySchema, new[] { "Bearer" } }
};
c.AddSecurityRequirement(securityRequirement);
});
}
我真的不明白这是什么问题?谁能帮帮我?
更改了 startup.cs 中的 Configure 方法,如下所示
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "AllStore.Api v1"));
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
而不是
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "AllStore.Api v1"));
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}