7

I have a .NETStandard library & need to add (versioned) JSON files by configuration-environment. The trick is...I want to see if it is possible to setup the Project File (.proj) to list them in the same manner they would if it was a Web.Config file.

FOR EXAMPLE:
A web.config will display this way in Visual Studio
Web Config by-environment

It accomplishes this by doing the following in the .PROJ file:

<None Include="Web.Debug.config">
  <DependentUpon>Web.config</DependentUpon>
</None>
<None Include="Web.ModelOffice.config">
  <DependentUpon>Web.config</DependentUpon>
</None>
<None Include="Web.Release.config">
  <DependentUpon>Web.config</DependentUpon>
</None>
<None Include="Web.Development.config">
  <DependentUpon>Web.config</DependentUpon>
</None>
<None Include="Web.Production.config">
  <DependentUpon>Web.config</DependentUpon>
</None>

So, to be clear...

Desired Effect

But this does NOT work in a .NETStandard library...

<None Include="appsettings.json" />
<None Include="appsettings.development.json">
  <DependentUpon>appsettings.json</DependentUpon>
</None>
<None Include="appsettings.modeloffice.json">
  <DependentUpon>appsettings.json</DependentUpon>
</None>
<None Include="appsettings.production.json">
  <DependentUpon>appsettings.json</DependentUpon>
</None>
Prisoner ZERO
  • 13,848
  • 21
  • 92
  • 137

2 Answers2

12

In .NET 5, you can do this via the DependentUpon tag in your .csproj file.

<ItemGroup>
  <None Update="appsettings.json">
    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
  </None>
</ItemGroup>
<ItemGroup>
  <None Update="appsettings.Production.json">
    <DependentUpon>appsettings.json</DependentUpon>
  </None>
</ItemGroup>
stuartd
  • 70,509
  • 14
  • 132
  • 163
2
  1. Integrated file nesting in Solution Explorer

  2. There is a FileNesting extension, written by Mads Kristensen.

    But beware, this extension has some limitations on the following project types (from the Known issues section of the extension description):

    • Node.js projects (NTVS)
    • ASP.NET Core (has built in rules for nesting)
    • Apache Cordova
    • Shared projects

In an unsupported project type (e.g. .NETStandard library), add the following to your project file & the "Add Custom Settings" option will automatically appear in your "Solution Explorer" toolbar. (Note that this is a workaround and not an official solution)

  <!-- I added this node to enable "Custom File Nesting" -->
  <ItemGroup>
    <ProjectCapability Include="DynamicDependentFile" />
    <ProjectCapability Include="DynamicFileNesting" />
  </ItemGroup>

After that...

  • Name your custom files accordingly (settings.json, settings.debug.json etc)
  • And set the "Standard Settings" option to "Web" in the "Add Custom Settings" option
kapsiR
  • 2,720
  • 28
  • 36