使用AspNetCore为IIS托管的web应用程序设置swagger。.json页面会加载,并且似乎可以正常使用所有的/swagger,但是当导航到{localhost} API查看UI页面时,我收到了一个404错误。我在Startup.cs中有以下代码:
//Configure Services
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "API ", Version = "v1" });
c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
});
}
//Configure
app.UseSwagger();
app.UseStaticFiles();
app.UseDeveloperExceptionPage();
// Enable middleware to serve generated Swagger as a JSON endpoint.
// 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;
});
app.UseMvc();
}有没有人看到这有什么潜在的问题?或者在故障排除方面有什么建议?我已经在这上面工作了一段时间了,希望有一双新的眼睛能看到我不是的东西。
发布于 2019-07-16 00:24:28
在MS Docs中,如果您希望在web应用程序的根目录中提供swagger UI,请将routePrefix设置为空字符串。
要在应用程序根目录(http://localhost:/)提供Swagger UI,请将RoutePrefix属性设置为空字符串
默认值是"/swagger";看起来您正在覆盖它,这就是UI不显示在“/swagger”上的原因。只需点击"localhost:“,UI就会显示出来。
删除routePrefix的赋值,它应该会像您预期的那样显示在"/swagger“中。
发布于 2019-07-16 15:07:23
尝试使用SwaggerEndpoint的相对路径:
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("../swagger/v1/swagger.json", "My API V1");
});然后导航到{App Url}/swagger。
请参阅https://github.com/domaindrivendev/Swashbuckle/issues/971
https://stackoverflow.com/questions/57043344
复制相似问题