78

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

Sinaesthetic
  • 11,426
  • 28
  • 107
  • 176

4 Answers4

157

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.

Marcel Gosselin
  • 4,610
  • 2
  • 31
  • 54
General Mac
  • 1,846
  • 1
  • 11
  • 15
  • Aha! I knew it was an item group, but was missing the other parts. Thanks! – Sinaesthetic Aug 28 '18 at 04:40
  • 1
    Also, fwiw, it turns out that you don't need to declare the parent separately. The first of the two `Content` nodes here is unnecessary. Just list them all in the `Include` attribute and set the `DependentUpon`. – Sinaesthetic Mar 09 '19 at 23:39
  • @Sinaesthetic Note to anyone reading this answer, I did require the first of the two content nodes. I suspect in your case it was elsewhere in the proj file. Otherwise, `appsettings.json` disappears entirely. – onefootswill Mar 25 '19 at 00:00
  • @onefootswill did you include appsettings.json in your `Include=` attribute? This works for me everywhere and it is not defined anywhere else. I've been doing it for months now. – Sinaesthetic Apr 01 '19 at 21:29
  • This solution will work. Another way to do it is by creating your own customized file nesting config in the Solution Explorer. See https://learn.microsoft.com/en-us/visualstudio/ide/file-nesting-solution-explorer?view=vs-2019 – Sarel Esterhuizen Oct 10 '19 at 12:11
  • Good to know but nesting config ms page states: "The feature is currently only supported for ASP.NET Core projects." and not for console projects as the OP is asking about. – Zach Feb 18 '20 at 15:43
  • I edited the second item to use globbing pattern `*` to get all files in one shot instead manually adding each file. – Marcel Gosselin Mar 11 '21 at 15:46
  • This method of nesting appsettings files still is currently supported. I've tested this with a .net 6 console app using Visual Studio 2022. – General Mac Jul 21 '22 at 15:44
  • I needed to change `` for `` in Visual Studio 2022 for Mac. – gabrielmaldi Aug 31 '22 at 21:57
14

enter image description here

You just need to click the File Nesting icon, and choose "Web"

daniell89
  • 1,832
  • 16
  • 28
9

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.

Yahoo Serious
  • 3,728
  • 1
  • 33
  • 37
0

This works for me in VS2022 (inspired by Yahoo Serious's answer):

  <ItemGroup>
    <Content Remove="appsettings.*.json" />
    <Content Include="appsettings.*.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <DependentUpon>appsettings.json</DependentUpon>
    </Content>
  </ItemGroup>
user488399
  • 44
  • 3