I keep seeing people put HTTPS 配置,包括
program.cs
内的 url 和证书路径。我知道我不必将实际的字符串文字留在那里,并且可以从其他地方提取它们,但它仍然让我觉得这样做是一个奇怪的地方。
显然,所有必要的东西都可以在
appsettings.json
中配置,而无需触及任何已编译的代码。这感觉好多了,但由于 appsettings.json
与应用程序一起发布,我宁愿让它不了解此类环境问题。理想情况下,我可以通过将 dotnet publish
的整个输出放在其顶部并重新启动 Kestrel 来更新应用程序,这将覆盖 appsettings.json。
我也想将其保留在 appsettings.json 之外,因为这在开发环境中很麻烦,我不关心 https。
说实话,我想让用户在
dotnet run
中配置特定于其托管环境的所有内容,包括他们是否需要 https,就像您可以使用 --urls
绑定到 url/端口一样。
所以我的问题是,是否有一个
dotnet run
相当于这个 appsettings.json?
"Kestrel": {
"EndPoints": {
"Http": {
"Url": "http://example.com:80"
},
"Https": {
"Url": "https://example.com:443",
"Certificate": {
"Subject": "example.com",
"Store": "webhosting"
"Location": "LocalMachine"
},
}
}
},
谢谢!
事实上,
appsettings.json
中的所有内容都可以转换为dotnet run
开关(以及环境变量)。
您的示例配置如下所示(为了便于阅读而添加换行符):
--Kestrel:Endpoints:Https:Url=https://*:443
--Kestrel:Endpoints:Https:Certificate:Subject=example.com
--Kestrel:Endpoints:Https:Certificate:Store=webhosting
--Kestrel:Endpoints:Https:Certificate:Location=LocalMachine"
您也许可以发现其中的规律。对于环境变量,您可以使用
__
而不是 :
。
有关更多信息,请参阅这篇精彩的文章:https://www.paraesthesia.com/archive/2018/06/20/microsoft-extensions-configuration-deep-dive/