Search code examples
json.net-coreconfigurationappsettingssln-file

Aggregating .Net Core appsettings.json


I have multiple project under the same solution. My concern is the following:

Is there any way to aggregate all appsettings.json from different projects into one file in the same solution ?


Solution

  • If you are using Visual Studio, use may use "Add as Link" feature (for other code editors, change .csproj manually as described after image):

    • put your 'appsettings.json' into solution folder (one level higher from project folder) for example;
    • for each project in solution:
      1. right-click project name in Solution Explorer
      2. in context menu: Add -> Existing item (Shift + Alt + A)
      3. navigate to appsettings.json file
      4. use drop-down of "Add" button and click "Add as link" (see screenshot below)
      5. you will see "appsettings.json" added to your project with small arrow on icon
      6. right-click that file, click Properties, and set "Copy to output directory" to "Copy always" or "Copy if newer".

    Image of "Add as Link" button option

    After you save you project, you will see this new lines in .csproj file (add them manually if you use code editor other than VS, without "Add as link" option):

    <ItemGroup>
      <Content Include="..\appsettings.json" Link="appsettings.json">
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      </Content>
    </ItemGroup>
    

    After that, building that project (from VS or with 'dotnet build') puts "appsettings.json" into your output build folder.