在 Azure 中部署时,Swagger UI 不会在 .net core 应用程序中创建,但它在本地运行良好
我已将其添加到
startup.cs中的ConfigureServices(IServiceCollection services)方法
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info
{
Version = "v1",
Title = "API",
Description = "API"
});
});
和
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "API");
c.RoutePrefix = "swagger";
});
app.UseAuthentication();
env.ConfigureNLog("nlog.config");
loggerFactory.AddNLog();
app.UseSignalR(routes =>
{
routes.MapHub<DashboardHub>("/hubs/dashboard");
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action=Index}/{id?}");
});
app.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
spa.UseReactDevelopmentServer(npmScript: "start");
}
});
在配置中(IApplicationBuilder 应用程序,IHostingEnvironment env,ILoggerFactory loggerFactory)
最佳答案
确保代码不是 IF 条件 (env.IsDevelopment())。 这就是为什么只能在本地主机上工作的原因。这避免了在生产时间工作。
像示例一样去掉swagger初始化。
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
// NOT HERE
}
// Enable middleware to serve generated Swagger as a JSON endpoint.
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");
c.RoutePrefix = string.Empty;
});
关于c# - Swagger UI 未在 azure .net core 中生成,但它在本地工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57674083/