[英]Swagger 'swagger.json' loads, but 404 error on swagger UI '{localhost}/swagger' in AspNet project
Working on setting up swagger for a web application hosted with IIS using AspNetCore.使用 AspNetCore 为 IIS 托管的 web 应用程序设置 swagger。 The.json page loads and seems to be touching all the API just fine, however when navigating to {localhost}/swagger to view the UI page I receive a 404 error. .json 页面加载并且似乎触及所有 API 就好了,但是当导航到 {localhost}/swagger 查看 UI 页面时,我收到 404 错误。 I have the following code in Startup.cs:我在 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();
}
Does anyone see any potential issues with this?有没有人看到任何潜在的问题? Or have any suggestions in terms of troubleshooting?或者在故障排除方面有什么建议? Have been working on this for a while now, hopefully a fresh set of eyes can see something I'm not.已经为此工作了一段时间,希望有新的眼睛能看到我没有看到的东西。
From the MS Docs , you set the routePrefix to an empty string if you want the swagger UI to be served at the root of your web app.从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要在应用程序的根 ( http://localhost :/) 处提供 Swagger UI,请将 RoutePrefix 属性设置为空字符串
The default is "/swagger";默认为“/swagger”; it looks like you're overriding this, which is why the UI is not showing at "/swagger."看起来你正在覆盖这个,这就是为什么 UI 没有显示在“/swagger”。 Just hit "localhost:" and the UI should show up.只需点击“localhost:”,UI 就会出现。
Remove that assignment of the routePrefix and it should show up at "/swagger" like you expect.删除 routePrefix 的分配,它应该像您期望的那样显示在“/swagger”中。
Try to use a relative path to SwaggerEndpoint:尝试使用 SwaggerEndpoint 的相对路径:
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("../swagger/v1/swagger.json", "My API V1");
});
And then navigate to {App Url}/swagger.然后导航到 {App Url}/swagger。
Refer to https://github.com/domaindrivendev/Swashbuckle/issues/971参考https://github.com/domaindrivendev/Swashbuckle/issues/971
Try to use a relative path to SwaggerEndpoint:尝试使用 SwaggerEndpoint 的相对路径:
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("../swagger/v1/swagger.json", "My API V1");
});
For experiment try adding the next line in your ~\Properties\launchSettings.json
file:对于实验,请尝试在~\Properties\launchSettings.json
文件中添加下一行:
"launchUrl": "swagger/index.html"
Finally it will be look like this:最后它看起来像这样:
In Startup.cs
remain app.UseSwaggerUI
with the next parameters:在Startup.cs
中,使用以下参数保留app.UseSwaggerUI
:
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("v1/swagger.json", "API V1");
c.InjectStylesheet("../swagger-ui/custom.css");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.