.NET Aspire仪表板无API端点URL,无法访问SwaggerUI
.NET Aspire仪表板无API端点URL问题排查与解决
给带有Swagger的ASP.NET Core Web API集成.NET Aspire后,编译正常、功能运行无异常,但Aspire仪表板的端点区域未显示任何URL,导致无法通过仪表板访问Swagger UI。相关配置如下:
现有配置
AppHost的Program.cs
var builder = DistributedApplication.CreateBuilder(args); builder.AddProject<Projects.API>("api", launchProfileName: "IIS Express"); builder.Build().Run();
AppHost的launchSettings.json
{ "$schema": "https://json.schemastore.org/launchsettings.json", "profiles": { "https": { "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, "applicationUrl": "https://localhost:17025;http://localhost:15225", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development", "DOTNET_ENVIRONMENT": "Development", "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21274", "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22163" } }, "http": { "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, "applicationUrl": "http://localhost:15225", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development", "DOTNET_ENVIRONMENT": "Development", "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19144", "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20276" } } } }
API的launchSettings.json
{ "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iis": { "applicationUrl": "http://localhost:44379/swagger/index.html", "sslPort": 0 }, "iisExpress": { "applicationUrl": "http://localhost:55448", "sslPort": 0 } }, "$schema": "http://json.schemastore.org/launchsettings.json", "profiles": { "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }, "nativeDebugging": false }, "API": { "commandName": "Project", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } } }
解决步骤
1. 替换IIS Express启动配置为项目自身配置
Aspire对IIS Express的端点检测支持有限,建议直接使用API项目的默认启动配置。修改AppHost的Program.cs,移除launchProfileName参数或指定为API项目中的"API"配置:
var builder = DistributedApplication.CreateBuilder(args); // 移除launchProfileName,使用API项目的默认启动配置 builder.AddProject<Projects.API>("api"); // 或指定为API项目中的"API" profile // builder.AddProject<Projects.API>("api", launchProfileName: "API"); builder.Build().Run();
2. 确保API项目集成Aspire服务端点
检查API项目的Program.cs,确认已添加Aspire核心服务和端点映射,这是Aspire识别API端点的关键:
var builder = WebApplication.CreateBuilder(args); // 添加Aspire服务默认配置 builder.AddServiceDefaults(); // 原有Swagger配置 builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); var app = builder.Build(); // 开发环境启用Swagger if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseHttpsRedirection(); // 映射Aspire默认端点(健康检查、服务发现等) app.MapDefaultEndpoints(); // 你的API业务端点映射 // app.MapGet("/api/xxx", () => ...); app.Run();
3. 完善API项目的启动URL配置
在API的launchSettings.json中,给"API" profile指定明确的applicationUrl,确保Aspire能正确抓取端点地址:
"API": { "commandName": "Project", "launchBrowser": true, "applicationUrl": "https://localhost:5001;http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }
4. 重启应用与仪表板
修改配置后,完全关闭所有相关进程(包括IIS Express、Aspire仪表板),重新启动AppHost项目,确保配置生效。
内容的提问来源于stack exchange,提问作者user3552264
相关产品推荐
相关产品推荐

