appsettings.json configuration per stage

This raises the need for efficient management of the configuration per stage and this is where appsettings.json comes into play with ASP NET Core. Have a read here if you are not familiar with it. To my surprise while everything worked and the final package would work on the deployed stage, no matter the build configuration, the build command would output all six files listed on the picture above. Of course this is a no-go and a major security concern.

Build folder using Debug configuration. Every appsettings.json configuration is copied.

Fixing this is a 3-step guide

1) If you have not created separate configurations for your solutions start by right-clicking the solution and bring up the Configuration Manager, click on Active Configuration and proceed to create a new one eg. QA and make sure to tick the checkbox underneath

New configuration Dialog

2) On your project set the build action of appsettings.json files to None. It should look like

Build action set to none.

3) Finally right-click your project and select Edit project file, before the closing </Project> tag add the following block of code which will act an an if-condition during the build step. Based on the selected configuration the specified appsettings.json will be copied.

<When Condition="'$(Configuration)' == 'Debug'">
    <None Include="appsettings.QA.json" CopyToOutputDirectory="Never" CopyToPublishDirectory="Never" />
    <None Include="appsettings.Development.json" CopyToOutputDirectory="Always" CopyToPublishDirectory="Always" />

<When Condition="'$(Configuration)' == 'QA'">
    <None Include="appsettings.Development.json" CopyToOutputDirectory="Never" CopyToPublishDirectory="Never" />
    <None Include="appsettings.QA.json" CopyToOutputDirectory="Always" CopyToPublishDirectory="Always" />
build using QA configuration

You can find a working example here:

Thanks for reading.