Can files be nested in VS2017 Solution Explorer for .NET Core (non-ASP.NET Core) projects?

Msbuild.Net CoreVisual Studio-2017Csproj

Msbuild Problem Overview


In "old school" MSBuild projects - as still used by Windows Forms in VS2017 for example - files could be "nested" via a DependentUpon item in the csproj file.

I used this to group unit tests together in Noda Time, e.g.

<Compile Include="LocalDateTest.PeriodArithmetic.cs">
  <DependentUpon>LocalDateTest.cs</DependentUpon>
</Compile>

That led to easily-navigable tests:

Nested tests

I knowingly "lost" this feature when moving to project.json for .NET Core, but had hoped it would return when converting to MSBuild. However, it looks like MSBuild projects based on the .NET Core SDK (root element <Project Sdk="Microsoft.NET.Sdk">) don't get the same treatment in Visual Studio 2017, even if an ItemGroup is added manually with the same elements as the "old school" project.

ASP.NET Core projects receive automatic nesting for minified CSS and Javascript, but it's not clear how to apply that to C# in .NET Core library projects.

Msbuild Solutions


Solution 1 - Msbuild

I have it working in one of my Microsoft.NET.Sdk-style projects using something similar to the following:

<ItemGroup>
  <Compile Update="LocalDateTest.*.cs">
    <DependentUpon>LocalDateTest.cs</DependentUpon>
  </Compile>
</ItemGroup>

The trick here is to use Update instead of Include. This is because the implicit items are coming from a props file that is imported before the main project. An additional Include won't affect files that are already included, but they can be modified using Update.

Solution 2 - Msbuild

In Visual Studio 2019, I have a .NET Core 2.2 <Project Sdk="Microsoft.NET.Sdk"> project in which I wanted the nicely-nested appsettings.json / appsettings.Development.json files, just like they do automatically for <Project Sdk="Microsoft.NET.Sdk.Web"> projects.

Here's what I had to add to the .CSPROJ:

  <ItemGroup>
    <Content Include="appsettings.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
    <Content Include="appsettings.Development.json">
      <DependentUpon>appsettings.json</DependentUpon>
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

After that, I had to unload/reload the project for the change to take effect in Solution Explorer. Note that I also set these files to always be copied to the output directory.

Solution 3 - Msbuild

If you want to wildcard it instead of hand-crafting each entry, adding these lines to your .csproj file means that anything like Foo.tests.cs automagically gets nested under Foo.cs

Tested and working in VS2019 with .NET Core 3.1.0

  <ItemGroup>
    <Compile Update="**\*.tests.cs">
      <DependentUpon>$([System.String]::Copy(%(Filename)).Replace(".tests",".cs"))</DependentUpon>
    </Compile>
  </ItemGroup>

Solution 4 - Msbuild

If you using .netstandardx.x you can not use [NestedIn](https://marketplace.visualstudio.com/items?itemName=MadsKristensen.FileNesting "NestedIn") . It's not working.

You can do that manually in your .csproj

<ItemGroup><Compile Include="BaseClass">ChildClass.cs</Compile></ItemGroup>

Solution 5 - Msbuild

If you use the same prefix it will nest files automatically.

Example:

AsemblyInfo.cs 
AsemblyInfo.local.cs

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJon SkeetView Question on Stackoverflow
Solution 1 - MsbuildMatthew KingView Answer on Stackoverflow
Solution 2 - MsbuildDavid McClellandView Answer on Stackoverflow
Solution 3 - MsbuildJinlyeView Answer on Stackoverflow
Solution 4 - MsbuildBatuhanView Answer on Stackoverflow
Solution 5 - MsbuildMiguel DominguesView Answer on Stackoverflow