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
4 Answers
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.

- 4,610
- 2
- 31
- 54

- 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
-
1Also, 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 ` – gabrielmaldi Aug 31 '22 at 21:57` in Visual Studio 2022 for Mac.
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?
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.

- 3,728
- 1
- 33
- 37
-
8In VS 2019 you just need to click the 'file nesting' button at the top of Solution Explorer. https://learn.microsoft.com/en-us/visualstudio/ide/file-nesting-solution-explorer?view=vs-2019 – GMK Aug 05 '21 at 04:33
-
-
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>

- 44
- 3