5
votes

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?

2
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
How did you specify the Test environemnt while publishing? - Edward

2 Answers

2
votes

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.

1
votes

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.