正在使用 AspNetCore 为使用 IIS 托管的 Web 应用程序设置 swagger。 .json 页面加载并且似乎可以很好地接触所有 API,但是当导航到 {localhost}/swagger 以查看 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();
}
有没有人看到任何潜在的问题?或者在故障排除方面有什么建议?已经在这方面工作了一段时间,希望有一双新的眼睛可以看到我没有看到的东西。
最佳答案
来自 MS Docs ,如果您希望在 Web 应用程序的根目录提供 swagger UI,则将 routePrefix 设置为空字符串。
To serve the Swagger UI at the app's root (http://localhost:/), set the RoutePrefix property to an empty string
默认为“/swagger”;看起来你正在覆盖这个,这就是为什么 UI 没有显示在“/swagger”。只需点击“localhost:”,UI 就会出现。
删除 routePrefix 的分配,它应该像您期望的那样显示在“/swagger”中。
关于c# - Swagger 'swagger.json' 加载,但在 AspNet 项目中的 swagger UI '{localhost}/swagger' 上出现 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57043344/