Copy configuration appsettings.json on build
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.
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
2) On your project set the build action of appsettings.json files to None. It should look like
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'">
<ItemGroup>
<None Include="appsettings.QA.json" CopyToOutputDirectory="Never" CopyToPublishDirectory="Never" />
<None Include="appsettings.Development.json" CopyToOutputDirectory="Always" CopyToPublishDirectory="Always" />
</ItemGroup>
</When>
<When Condition="'$(Configuration)' == 'QA'">
<ItemGroup>
<None Include="appsettings.Development.json" CopyToOutputDirectory="Never" CopyToPublishDirectory="Never" />
<None Include="appsettings.QA.json" CopyToOutputDirectory="Always" CopyToPublishDirectory="Always" />
</ItemGroup>
</When>
You can find a working example here: https://github.com/jmpatsou/output-appsettings-demo
Thanks for reading.