How to publish environment specific appsettings in .Net core app?

后端 未结 12 2452
刺人心
刺人心 2020-12-04 15:33

I have 3 environment specific appsettings files in my .Net core application

in project.json I have setup publishOptions

12条回答
  •  感情败类
    2020-12-04 16:06

    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:

    
      
      
      
      
    
    

    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)

提交回复
热议问题