Windows下dotnet run仅监听HTTP未监听HTTPS的问题排查求助
问题描述
在Windows系统上运行ASP.NET程序,执行dotnet run命令后,程序仅监听http://localhost:5027,未监听HTTPS端口。已执行dotnet dev-certs https --trust信任开发证书,但问题依旧。
命令执行输出
PS C:\Users\test\Documents\TodoApi> dotnet run Building... info: Microsoft.Hosting.Lifetime[14] Now listening on: http://localhost:5027 info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl+C to shut down. info: Microsoft.Hosting.Lifetime[0] Hosting environment: Development info: Microsoft.Hosting.Lifetime[0] Content root path: C:\Users\test\Documents\TodoApi
launchsettings.json 内容
{ "$schema": "https://json.schemastore.org/launchsettings.json", "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:40409", "sslPort": 44321 } }, "profiles": { "http": { "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, "applicationUrl": "http://localhost:5027", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "https": { "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, "applicationUrl": "https://localhost:7130", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } } }
解决方案
- 指定HTTPS启动配置文件:执行
dotnet run --launch-profile https,直接调用launchsettings中的https配置项,程序会监听https://localhost:7130。 - 检查Program.cs中的HTTPS配置:确保代码中启用了HTTPS重定向和监听,添加以下代码:
// 配置HTTPS重定向 builder.Services.AddHttpsRedirection(options => { options.HttpsPort = 7130; }); // 在中间件管道中启用HTTPS重定向 app.UseHttpsRedirection(); - 设置环境变量强制监听HTTPS:设置
ASPNETCORE_URLS环境变量为https://localhost:7130;http://localhost:5027,这样dotnet run会同时监听两个端口。在PowerShell中执行:$env:ASPNETCORE_URLS="https://localhost:7130;http://localhost:5027" dotnet run - 确认launchsettings.json位置:确保该文件位于项目的
Properties文件夹下,否则dotnet run无法读取配置。
内容的提问来源于stack exchange,提问作者sunny kumar
相关产品推荐
相关产品推荐

