ASP.NET Core 7 Minimal API默认不跳转Swagger问题求助
ASP.NET Core 7 Minimal API 默认打开Swagger配置问题
我们开发了一个ASP.NET Core 7 Minimal API应用,启动后未自动跳转至Swagger界面,反而出现“Localhost not found”错误;但在URL末尾添加/swagger路径后,Swagger可正常打开。请问要设置默认打开Swagger,是否遗漏了相关配置?
以下是当前program.cs代码:
using Dapr.Client; using ErpInterfaceService.Abstractions; using ErpInterfaceService.Data; using ErpInterfaceService.Models; using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); builder.Services.AddEndpointDefinitions(typeof(Message)); builder.Services.AddHealthChecks(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); // TODO: Move this connection string to a secrets store. builder.Services.AddDbContext<ErpInterfaceContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("ErpInterface"))); builder.Services.AddSingleton(new DaprClientBuilder().Build()); var app = builder.Build(); // Apply any new migrations to the database using (var scope = app.Services.CreateScope()) { var dbContext = scope.ServiceProvider.GetRequiredService<ErpInterfaceContext>(); dbContext.Database.Migrate(); } if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseEndpointDefinitions(); app.MapHealthChecks("/health"); app.Run();
解决方案
你需要添加两个关键配置之一(或两者结合)来实现启动自动跳转Swagger:
方法1:配置SwaggerUI默认路由跳转
在UseSwaggerUI中添加配置,让根路径/自动重定向到Swagger界面:
if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(options => { // 设置根路径直接指向Swagger文档页面 options.RoutePrefix = string.Empty; options.SwaggerEndpoint("/swagger/v1/swagger.json", "ErpInterfaceService API V1"); }); }
方法2:修改launchSettings.json设置启动URL
找到项目中的Properties/launchSettings.json,修改launchUrl字段,指定启动时直接打开/swagger:
{ "profiles": { "http": { "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, "launchUrl": "swagger", "applicationUrl": "http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } } }
修改后的完整program.cs示例
using Dapr.Client; using ErpInterfaceService.Abstractions; using ErpInterfaceService.Data; using ErpInterfaceService.Models; using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); builder.Services.AddEndpointDefinitions(typeof(Message)); builder.Services.AddHealthChecks(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); // TODO: Move this connection string to a secrets store. builder.Services.AddDbContext<ErpInterfaceContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("ErpInterface"))); builder.Services.AddSingleton(new DaprClientBuilder().Build()); var app = builder.Build(); // Apply any new migrations to the database using (var scope = app.Services.CreateScope()) { var dbContext = scope.ServiceProvider.GetRequiredService<ErpInterfaceContext>(); dbContext.Database.Migrate(); } if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(options => { options.RoutePrefix = string.Empty; options.SwaggerEndpoint("/swagger/v1/swagger.json", "ErpInterfaceService API V1"); }); } app.UseEndpointDefinitions(); app.MapHealthChecks("/health"); app.Run();
内容的提问来源于stack exchange,提问作者John Swaringen
相关产品推荐
相关产品推荐

