Search code examples
visual-studio-2017.net-coreasp.net-core-2.0

How do I get a console project to group my appsettings.json files?


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


Solution

  • 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.