我在 VS 2022 中创建了一个 ASP.NET Core 6.0 应用程序。
当它在调试模式下构建时,它会在 bin/Debug/net6.0 目录中创建一堆文件。
其中之一是 .exe
我可以运行它,它会打开一个紫色的命令行窗口,它报告在 Microsoft.Hosting.Lifetime 中运行并显示它运行的 url。
我假设可执行文件启动了一个 Kestrel 实例,其中包含我的 ASP.NET Core 应用程序?
在 Visual Studio 中运行此应用程序会导致应用程序托管在端口上,我可以追溯到 launchSettings.json 文件。
info: Microsoft.Hosting.Lifetime[14]
Now listening on: https://localhost:7053
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5105
显然我可以在 launchSettings.json 中更改这些端口,但它只有在我在 VS 中运行时才有效
如果我改为运行 exe 文件(bin\Debug 中的那个
et6.0),端口始终为 5000 和 5001
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[14]
Now listening on: https://localhost:5001
我如何配置这些端口(所以我可以指定不同的数字而不是 5000 和 5001?
我也可以让 url 包含一些名称而不是
http://localhost:5000 and https://localhost:5001
是
http://localhost/MyApp1 and https://localhost/MyApp1
?
感谢 Quing Guo 发布的链接
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/endpoints?view=aspnetcore-6.0
从 bin\Debug 运行 exe 时配置端口的答案 et6.0 目录是将以下部分添加到 appsettings.json(在同一目录中):
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://localhost:5002"
},
"Https": {
"Url": "https://localhost:5003"
}
}
},
问题的第二部分(如何从 https://{server}:{port} 重定向到 https://{server}/{path} 仍然没有答案,但可以在这一部分中解决:
我如何配置这些端口(所以我可以代替 5000 和 5001 指定不同的数字?
是的,你可以。
我也可以让 url 包含一些名字吗?
对不起,也许你不能, 你可以像这样设置网址:
{scheme}://{loopbackAddress}:{port}
从为ASP.NET Core Kestrel web服务器配置端点我们可以看到:
ASP.NET Core 项目被配置为绑定到一个随机的 HTTP 端口 5000-5300 之间和 7000-7300 之间的随机 HTTPS 端口。这 默认配置在生成的 Properties/launchSettings.json 文件,可以被覆盖。
在您的launchSettings.json文件中覆盖
applicationUrl
:
"applicationUrl": "https://localhost:8001;http://localhost:8000"
结果:
更新:
具体运行位于bin\Debug的exe文件 et6.0 在那 case launchSettings.json 没有效果
这种情况下,可以参考Configure(IConfiguration)在appsettings.json中设置端口:
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://localhost:5000"
},
"Https": {
"Url": "https://localhost:5001"
}
}
}