Search code examples
visual-studiocsprojsln-file

How to include an <ItemGroup> across multiple .csproj files


Within Visual Studio (2019 in this case) solution, is there a way to specify an <ItemGroup> in a location that multiple .csproj files can use it within the solution?

As an example, this ItemGroup is used in multiple .csproj files within Test.sln. Instead of having to add this ItemGroup to each .csproj file, I'd like to somehow place it in a common file and reference it from the .csproj file. Is that possible?

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.10" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdk)" />
  </ItemGroup>

Solution

  • You can use a Directory.Build.props file in the directory hierarchy (e.g. next to the .sln file) containing only these common things:

    <Project>
      <ItemGroup>
        ...
      </ItemGroup>
    </Project>