Microsoft.Extensions.Hosting EnvironmentName覆盖默认生产值

时间:2019-01-15 20:40:20

标签: c# .net .net-core

Item

我在Docker文件中添加了新的全局env变量

var hostBuilder = new HostBuilder()
    .ConfigureHostConfiguration((config) =>
    {
        config.AddEnvironmentVariables();
    })
    .ConfigureAppConfiguration((hostContext, config) =>
    {
        config.SetBasePath(Environment.CurrentDirectory);
        config.AddJsonFile("appsettings.json", optional: false);
        config.AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", optional: true);
        config.AddEnvironmentVariables();
    });

但是我的ASPNETCORE_ENVIRONMENT = Development 始终是hostContext.HostingEnvironment.EnvironmentName

如何使用默认的Production值覆盖Microsoft.Extensions.Hosting中的EnvironmentName?

2 个答案:

答案 0 :(得分:0)

打开json文件launchsettings.json,然后查找以下部分:“配置文件”->“您的项目名称”->“ environmentVariables”->“ ASPNETCORE_ENVIRONMENT”,并将此值从“开发”更新为“生产”。

答案 1 :(得分:0)

如果这是一个选项,您还可以使用和json配置文件:

            var host = new HostBuilder()
                   .ConfigureHostConfiguration(builder =>
                   {
                       builder.AddJsonFile("hostsettings.json", optional: true);
                   })

{
  "environment": "Development",
}