Home > other >  Synchronize appsettings.json with Debug/Release build configuration
Synchronize appsettings.json with Debug/Release build configuration

Time:07-02

In my ASP.NET Core 6 project I have the following configuration files:

appsettings.json
appsettings.Development.json
appsettings.Production.json

Then in my Startup class I provide a constructor to get the configuration:

public Startup(IConfiguration config)
{
    this.Configuration = config;
}

I intend to use appsettings.json appsettings.Development.json config in Debug and appsettings.json appsettings.Production.json in Release.

I achieved this by using the following code in my CreateHostBuilder:

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureLogging(logging =>
        {
           // ..
        })
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        })
#if DEBUG
        .UseEnvironment("Development");
#else
        .UseEnvironment("Production");
#endif
}

Is there a better way to do this?

CodePudding user response:

Sounds like you are looking for environment specific transformation of your configuration file. Check out How do I transform appsettings.json in a .NET Core MVC project?

  • Related