title: How to get a TestServer to use a different appsettings file?
body: <p>This is a pretty weird problem I'm having. I'm trying to set up a <code>TestServer</code> to test an ASP.NET API. In this particular test, instead of the <code>appsettings.json</code> of the project, I want to use a different config, <code>appsettings.custom.json</code>. So, when I create my <code>TestServer</code>, here's what I do:</p>
  
  <pre><code>protected TestServer CreateTestServer()
  {
      var testConfiguration = new AspNetTestConfiguration(); // configures middleware, authentication, default/static files
      var configFile = "appsettings.custom.json";
      var config = new ConfigurationBuilder().AddJsonFile(configFile).Build();
      Container.Configure(cfg =&gt; cfg.For&lt;IConfiguration&gt;().Use(config)); // attempt 1 - that's my StructureMap container used for DI
  
      // Create the WebHostBuilder used by the test server
      var webHostBuilder = WebHost.CreateDefaultBuilder()
                                  .ConfigureServices(svc =&gt;
                                                     {
                                                         svc.AddSingleton(Container);
                                                         svc.AddSingleton(config); // attempt 2
                                                         svc.AddSingleton(testConfiguration);
                                                     })
                                  .UseConfiguration(config) // attempt 3
                                  .UseStartup&lt;TestStartup&gt;();
  
      return new TestServer(webHostBuilder);
  }
  </code></pre>
  
  <p>But then, when I get into my controller, which has an <code>IConfiguration</code> provided in the constructor through dependency injection, I access the settings in the <code>IConfiguration</code> and find that the values are the ones from <code>appsettings.json</code>!</p>
  
  <p>As you can see above, I've tried 3 different ways to convince the <code>TestServer</code> that it must use config values from <code>appsettings.custom.json</code>, to no avail.</p>
  
  <p>Why is it still insisting on using <code>appsettings.json</code>, and how do I fix it?</p>
  
  <p><strong>UPDATE:</strong>
  Curiouser and curiouser. I tried renaming <code>appsettings.json</code> to <code>appsettings.default.json</code> and set that as my default settings file. Now, magically, my <code>TestServer</code> is using <code>appsettings.custom.json</code> where I expected it to!</p>