Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to publish only certain appsettings.json in .NetCore application

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?

like image 899
yyc Avatar asked Dec 19 '18 05:12

yyc


People also ask

Can I add more than two Appsettings json files in dotnet core?

Of course, we can add and use multiple appsettings. json files in ASP.NET Core project.

What should I store in Appsettings json?

The appsettings. json file is generally used to store the application configuration settings such as database connection strings, any application scope global variables, and much other information.

What is Launchsetting json in ASP.NET Core?

The launchSettings. json file is used to store the configuration information, which describes how to start the ASP.NET Core application, using Visual Studio. The file is used only during the development of the application using Visual Studio. It contains only those settings that required to run the application.


1 Answers

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.

like image 78
yyc Avatar answered Sep 23 '22 10:09

yyc