.net core 无法识别环境变量
最终赢得7分。NET内核,SDK 2.1,Web MVC项目 config.json.net core 无法识别环境变量,.net-core,environment-variables,windows-7-x64,.net Core,Environment Variables,Windows 7 X64,最终赢得7分。NET内核,SDK 2.1,Web MVC项目 config.json { "Colors": { "Favorite": "blue" }, "DisableSSL": false, "ConnectionStrings": {... }, "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning" }
{
"Colors": {
"Favorite": "blue"
},
"DisableSSL": false,
"ConnectionStrings": {...
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning"
}
}
}
Startup.cs
services.AddMvc(opt => { if (env.IsProduction() && config["DisableSSL"] != "true")
{ opt.Filters.Add(new RequireHttpsAttribute()); } })
.AddJsonOptions(opt => opt.SerializerSettings
.ReferenceLoopHandling = ReferenceLoopHandling.Ignore);
当我发布到文件夹并打开文件夹中的CLI,然后设置DisableSSl=true并从CLI运行应用程序,然后浏览到localhost:5000
时,我将被重定向到
为什么CLI中的禁用SSL
没有覆盖配置文件中的SSL
如果我注释掉.Add(newrequireHttpAttribute())代码>和重新发布并从CLI运行它不会重定向,我可以导航到localhost:5000上的网站,以便发布正常工作
我知道如何创建在SSL下运行的证书,这不是我的问题
如何让它识别CLI会话中设置的环境变量
(如果在系统属性下创建一个环境变量,禁用SSL
,其值true
。该变量也将被忽略,我将被重定向)。因此,我运行了一个测试,也许我发现了您的问题:
采用以下appsettings
:
{
"Test1": true,
"Test2": "true",
}
现在让我们来看看我们得到了什么:
var one = Configuration["Test1"];
//result: one == "True"
var two = Configuration["Test2"];
//result: two == "true"
因此,当您将环境变量设置为true时,我假设它显示为“true”,并且由于区分大小写“true”!=“true”
。尝试将其转换为小写。所有内容都已是小写。你的测试只是确认我所做的应该是有效的。我测试“true”,并在环境中设置DisableSSL=true。@您是否使用Powershell或CMD设置环境变量?CMD。从Powershell执行仍然重定向。@Joe您是否在Powershell中尝试了$Env:DisableSSL=“true”
?仍然重定向。