无法 运行 大摇大摆 UI
Not able to run Swagger UI
我有一个 .NetCore Web API 项目。我正在尝试在其上使用 Swagger。
所有配置看起来都不错,但是当我 运行 我的项目出现 404 错误时,找不到页面。
这是我的代码:
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton(_config);
services.AddTransient<IRestHelper, RestHelper>();
// Add framework services.
services.AddApplicationInsightsTelemetry(_config);
services.AddMvc();
services.AddSwaggerGen(config =>
{
config.SwaggerDoc("v1", new Info { Title = "Slack Relay API", Version = "v1" });
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(_config.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseApplicationInsightsRequestTelemetry();
app.UseApplicationInsightsExceptionTelemetry();
app.UseMvc();
app.UseSwagger(c =>
{
c.PreSerializeFilters.Add((swagger, httpReq) => swagger.Host = httpReq.Host.Value);
});
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "V1 Docs");
});
}
我的网站 API 基础 url 是:http://localhost:88/
所以当我尝试打开这个 URL 我得到一个 404: http://localhost:88/swagger/ui
我使用的是 NetCore 1.0.1,在 project.json 中,我用这一行来导入 Swagger:
"Swashbuckle.AspNetCore": "1.0.0"
有什么帮助吗?
谢谢
您需要记住中间件的顺序很重要:MVC 中间件应该是最后一个,因为它会尝试处理所有请求。由于您没有将控制器的操作映射到 /swagger/ui
,您会收到 404 错误。修改Configure
方法为:
app.UseSwagger(c =>
{
c.PreSerializeFilters.Add((swagger, httpReq) => swagger.Host = httpReq.Host.Value);
});
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "V1 Docs");
});
app.UseMvc();
此外,最好将 app.UseMvc() 替换为类似以下内容,这样 MVC 中间件将不会尝试处理任何与 swagger 相关的请求(对 URL 的请求以 /swagger
):
app.MapWhen(x => !x.Request.Path.Value.StartsWith("/swagger"), builder =>
{
builder.UseMvc();
});
您在浏览器中输入了错误的 URL。使用 Swagger 1.0.0 RTM,默认 url 从 http://localhost:88/swagger/ui to http://localhost:88/swagger
更改
我有一个 .NetCore Web API 项目。我正在尝试在其上使用 Swagger。 所有配置看起来都不错,但是当我 运行 我的项目出现 404 错误时,找不到页面。
这是我的代码:
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton(_config);
services.AddTransient<IRestHelper, RestHelper>();
// Add framework services.
services.AddApplicationInsightsTelemetry(_config);
services.AddMvc();
services.AddSwaggerGen(config =>
{
config.SwaggerDoc("v1", new Info { Title = "Slack Relay API", Version = "v1" });
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(_config.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseApplicationInsightsRequestTelemetry();
app.UseApplicationInsightsExceptionTelemetry();
app.UseMvc();
app.UseSwagger(c =>
{
c.PreSerializeFilters.Add((swagger, httpReq) => swagger.Host = httpReq.Host.Value);
});
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "V1 Docs");
});
}
我的网站 API 基础 url 是:http://localhost:88/ 所以当我尝试打开这个 URL 我得到一个 404: http://localhost:88/swagger/ui
我使用的是 NetCore 1.0.1,在 project.json 中,我用这一行来导入 Swagger: "Swashbuckle.AspNetCore": "1.0.0"
有什么帮助吗? 谢谢
您需要记住中间件的顺序很重要:MVC 中间件应该是最后一个,因为它会尝试处理所有请求。由于您没有将控制器的操作映射到 /swagger/ui
,您会收到 404 错误。修改Configure
方法为:
app.UseSwagger(c =>
{
c.PreSerializeFilters.Add((swagger, httpReq) => swagger.Host = httpReq.Host.Value);
});
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "V1 Docs");
});
app.UseMvc();
此外,最好将 app.UseMvc() 替换为类似以下内容,这样 MVC 中间件将不会尝试处理任何与 swagger 相关的请求(对 URL 的请求以 /swagger
):
app.MapWhen(x => !x.Request.Path.Value.StartsWith("/swagger"), builder =>
{
builder.UseMvc();
});
您在浏览器中输入了错误的 URL。使用 Swagger 1.0.0 RTM,默认 url 从 http://localhost:88/swagger/ui to http://localhost:88/swagger
更改