How do I get a console project to group my appsettings.json files?

If I start a new web api project, the appsettings files are grouped together. However, I'm creating a working project from the console app template and when I create the appsettings files manually, the do not group together. I think back in older versions, there was something I'd put in the csproj file, but I don't know how to do it in .net core and I'm not seeing anything in properties or configurations

enter image description here


In the project file of your solution you can edit or add an <ItemGroup> element within the <Project> element. This worked for me:

  <ItemGroup>
    <Content Include="appsettings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
    <Content Include="appsettings.*.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <DependentUpon>appsettings.json</DependentUpon>
    </Content>
  </ItemGroup>

Please Note that my console project targets .Net Core 2.0 and is running in Visual Studio Pro 2017 Version 15.7.5. Also, if your Solution Explorer doesn't immediately refresh try unloading and reloading the project.


Using an <ItemGroup> with <Content> as suggested gave me an error (in Visual Studio 2019) about "Duplicate 'Content' items included". It turns out the .NET SDK includes 'Content' items from your project directory by default. Setting the EnableDefaultContentItems property to false seems a bit rigid, so now I include the items as <None>.

<ItemGroup>
  <!-- Group AppSettings in Console project. Use None to prevent "Duplicate 'Content' items were included" when using (default) EnableDefaultContentItems=true -->
  <None Include="appsettings.*.json">
    <DependentUpon>appsettings.json</DependentUpon>
  </None>
</ItemGroup>

This does show the files grouped, but shows their properties with Build Action 'None' and 'Do Not Copy' in the Solution-explorer, so I guess that's the price for wanting them to group?

Grouped appsettings.staging.json with Build Action 'None' and 'Do Not Copy'

FWIW: a file-nesting rule as suggested in appsettings-json-not-in-hierarchy will not show the files as grouped/nested, but it will make it collapse if the solution-explorer collapse-button is pressed.