title: Load appsettings.{envName}.json file in ASP.NET Core 2.1
body: <p>In <a href="https://andrewlock.net/exploring-program-and-startup-in-asp-net-core-2-preview1-2/" rel="nofollow noreferrer">prior versions of ASP.NET Core</a> we could dynamically add appsetting.json files with the environment suffix like <code>appsettings.Production.json</code> for production environment. </p>
  
  <p>Since the structure is a bit different, it seems that the configuration now got loaded in class <code>Program</code>. But we dont have the `` injected here,so I tried it myself using environment variable:</p>
  
  <pre><code>public class Program {
          public static void Main(string[] args) {
              CreateWebHostBuilder(args).Build().Run();
          }
  
          public static IWebHostBuilder CreateWebHostBuilder(string[] args) {
              string envName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
              string envConfigFile = $"appsettings.{envName}.json";
  
              var config = new ConfigurationBuilder()
                  .SetBasePath(Directory.GetCurrentDirectory())
                  .AddJsonFile(envConfigFile, optional: true);
  
              var finalConfig = config.Build();
              return WebHost.CreateDefaultBuilder(args)
                  .UseUrls("http://0.0.0.0:5000")
                  .UseConfiguration(finalConfig)
                  .UseStartup&lt;Startup&gt;();
          }
      }
  </code></pre>
  
  <p>The code got executed but it doesn't override my <code>appsettings.json</code> config. Let's say I have the following <code>appsettings.json</code>:</p>
  
  <pre><code>{
      "MyConnectionString": "Server=xxx,Database=xxx, ..."
  }
  </code></pre>
  
  <p>This ConnectionString is working. Now I create<code>appsettings.Development.json</code></p>
  
  <pre><code>{
      "MyConnectionString": ""
  }
  </code></pre>
  
  <p>and set <code>ASPNETCORE_ENVIRONMENT=Development</code>. This should definitely throw an exception. But the app start correctly with the ConnectionString from <code>appsettings.json</code>. </p>
  
  <p>According to <a href="https://joonasw.net/view/aspnet-core-2-configuration-changes" rel="nofollow noreferrer">ASP.NET Core 2 configuration changes</a>, the <code>WebHost.CreateDefaultBuilder</code> method should itself use the new <code>ConfigureAppConfiguration</code> approach and load environment specific config by default. Also <a href="https://docs.microsoft.com/de-de/aspnet/core/fundamentals/host/web-host?view=aspnetcore-2.1" rel="nofollow noreferrer">the official docs</a> say that <code>appsettings.{Environment}.json</code> are loaded automatically. But this doesn't work and also loading it manually like this doesn't solve the problem: </p>
  
  <pre><code>return WebHost.CreateDefaultBuilder(args)
                  .UseUrls("http://0.0.0.0:5000")
                  .ConfigureAppConfiguration((builderContext, config) =&gt; {
                      IHostingEnvironment env = builderContext.HostingEnvironment;
                      config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                          .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
                  })
                  .UseStartup&lt;Startup&gt;();
  </code></pre>
  
  <p>How can I override the config using <code>"appsettings.{envName}.json"</code> pattern? I liked that cause it was handy to override env specific things in ASP.NET Core. </p>