【发布时间】:2019-11-13 12:33:08
【问题描述】:
尝试将 swagger 与托管在 IIS express 上的 Web 应用程序一起设置。 API 是使用 ASP Net Core 构建的。我已按照相关 Microsoft 帮助页面上关于 Swashbuckle 和 ASP.NET Core 的说明进行操作。
到目前为止,我已经加载了 swagger 页面,可以看到我定义的 SwaggerDoc 正在加载,但是没有 API 存在。目前出现以下错误:
“获取错误未定义./swagger/v1/swagger.json”
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// services.AddDbContext<TodoContext>(opt =>
// opt.UseInMemoryDatabase("TodoList"));
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
// Register the Swagger generator, defining 1 or more Swagger documents
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "API WSVAP (WebSmartView)", Version = "v1" });
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// 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;
});
app.UseMvc();
}
}
【问题讨论】:
-
您能解释一下您在启动时为配置类分配接口的情况吗?
-
使用
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); -
jPhizzle - 这是以前的故障排除尝试遗留下来的。我已经更新了代码。道歉
-
Akash KC - 我最初尝试过,不幸的是没有变化。
-
浏览器开发工具的控制台选项卡上的错误信息是什么?
标签: c# asp.net-mvc swagger-ui swashbuckle