环境变量无法识别

时间:2018-06-15 05:13:29

标签: .net-core environment-variables windows-7-x64

赢得7终极。 .NET Core,SDK 2.1,Web MVC项目。

config.json

{
  "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并set DisableSSl=true并从CLI运行应用程序,然后浏览到localhost:5000我被重定向到https://localhost。< / p>

为什么来自CLI的DisableSSL不会覆盖配置文件中的那个?

如果我注释掉.Add(new RequireHttpsAttribute());并重新发布并从CLI运行它不会重定向,我可以导航到localhost:5000的网站,以便发布工作正常。

我知道如何创建在SSL下运行的证书,这不是我的问题。

如何识别CLI会话中设置的环境变量?

(如果在Environment Variable System Properties下创建DisableSSL,其值为true。它也会被忽略,我会被重定向。)

1 个答案:

答案 0 :(得分:1)

所以我进行了测试,也许我找到了你的问题:

采取以下appsettings

{
  "Test1": true,
  "Test2": "true",
}

现在让我们看看我们得到了什么:

var one = Configuration["Test1"];
//result: one == "True"
var two = Configuration["Test2"];
//result: two == "true"

因此,当你将环境变量设置为true时,我假设它将其提升为&#34; True&#34;由于区分大小写"True" != "true"。尝试将其转换为小写。

相关问题