6

I have 4 appsettings.json in my .NetCore application:

  • appsettings.json
  • appsettings.Development.json
  • appsettings.Test.json
  • appsettings.Production.json

All of appsettings has Do not copy property, but I notice when publishing the application, all of appsettings files are copied over to publish folder. For example, appsettings.Production.json is copied to publish folder even I am publishing using Test environment.

It doesn't hurt but I want to know whether is it possible to copy just appsettings.json and appsettings.Test.json when publishing using Test environment?

yyc
  • 288
  • 1
  • 4
  • 16
  • It is possible to delete files before and after publishing in visual studio publish profile. You can create various publish profiles, each one deleting your specified appsettings after publishing. – Sasan Dec 19 '18 at 18:04
  • How did you specify the Test environemnt while publishing? – Edward Dec 20 '18 at 08:39
  • Does this answer your question? [How to publish environment specific appsettings in .Net core app?](https://stackoverflow.com/questions/39256057/how-to-publish-environment-specific-appsettings-in-net-core-app) – Ash K Sep 28 '22 at 14:49

2 Answers2

3

Finally, the trick is to use <Content Remove=""> for appsettings.json.

I updated my .csproj to use conditional constructor to switch between different environments. Here is what it looks like:

<ItemGroup>
  <!-- Default behaviour here -->
  <None Update="other_files">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
  </None>
</ItemGroup> 
<Choose>
  <When Condition=" '$(EnvironmentName)'=='Test' ">
    <ItemGroup>
      <Content Remove="appsettings.Development.json" />
      <Content Remove="appsettings.Production.json" />

      <!-- Other files you want to update in the scope of Debug -->  
      <None Update="other_files">
        <CopyToOutputDirectory>Never</CopyToOutputDirectory>
      </None>
    </ItemGroup>
  </When>
</Choose>

The folder now does not contain appsettings.Development.json and appsettings.Production.json when I run publish using Test environment.

yyc
  • 288
  • 1
  • 4
  • 16
2

While publishing, we could specify Configuration. To implement your requirement, you could define different configurations by Configuration Manager.

  1. Click Debug or Release Dropdown ->Configuration Manager -> New Active solution configuration for Development, Test and etc like DevelopmentPublish and TestPublish
  2. Modify project.csproj

    <Project Sdk="Microsoft.NET.Sdk.Web">
    
    <PropertyGroup>
        <TargetFramework>netcoreapp2.1</TargetFramework>
        <Configurations>Debug;Release;DevelopmentPublish</Configurations>
    </PropertyGroup>
    
    <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.App" />
        <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
    </ItemGroup>
    
    <Target Name="DevelopmentPublish" AfterTargets="AfterPublish" Condition="'$(Configuration)'!='DevelopmentPublish'">
        <Delete Files="$(ProjectDir)$(publishUrl)appsettings.Development.json" />
    </Target>
    <Target Name="TestPublish" AfterTargets="AfterPublish" Condition="'$(Configuration)'!='TestPublish'">
        <Message Text="TestPublish"></Message>
        <Delete Files="$(ProjectDir)$(publishUrl)appsettings.Test.json" />
    </Target>
    </Project>
    
  3. While publishing, choose the expected configuration for publish like TestPublish for test publish process.
Edward
  • 28,296
  • 11
  • 76
  • 121
  • 1
    I just created different publishing profiles for each environment and added the "delete target" directly to each with the specific files to remove for that environment. Thanks for the idea. Works well! – Craig Aug 24 '20 at 18:16