I have this same problem because I need to switch between different production configuration files due to multiple sites using the same codebase.
In the end, I created a powershell script for each client/site. The script copies a client-specific configuration file over the production configuration file, then runs the publish. I actually do this for both the appSettings.json file and the environment.ts file. The script looks a bit like this:
Remove-Item –path ClientApp\src\environments\environment.prod.ts
Remove-Item –path appsettings.production.json
Write-Output "--> config files removed"
Copy-Item -path ClientApp\src\environments\environment.CLIENT-SITE-NAME.ts ClientApp\src\environments\environment.prod.ts
Copy-Item -path appsettings.CLIENT-SITE-NAME.json appsettings.production.json
Write-Output "--> config files copied"
dotnet build MYPROJ.csproj -c Release /p:DeployOnBuild=true /p:PublishProfile=CLIENT-SITE-NAME
Write-Output "--> built & published"
Remove-Item –path ClientApp\src\environments\environment.prod.ts
Remove-Item –path appsettings.production.json
Write-Output "Finished"
In each of my client sites' .pubxml files, I exclude ALL the non-production appsettings from being published, like so:
<ItemGroup>
<Content Update="appsettings.json" CopyToPublishDirectory="Never" />
<Content Update="appsettings.site1.json" CopyToPublishDirectory="Never" />
<Content Update="appsettings.site2.json" CopyToPublishDirectory="Never" />
<Content Update="appsettings.development.json" CopyToPublishDirectory="Never" />
</ItemGroup>
I remove the production files in the end to make sure I don't accidentally deploy them to the wrong site using the Publish wizard.
(I store the password in the pubxml file, but you could include it in the script as a parameter as an alternative)
appsettings.json
andappsettings.{environmentname}.json
. The order in which you are loading files at startup is important here – Keever