I have 4 appsettings.json in my .NetCore application:
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?
Of course, we can add and use multiple appsettings. json files in ASP.NET Core project.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With