如何获取控制台项目以对appsettings.json文件进行分组?

Sin*_*tic 18 .net-core visual-studio-2017 asp.net-core-2.0

如果我启动一个新的web api项目,appsettings文件将组合在一起.但是,我正在从控制台应用程序模板创建一个工作项目,当我手动创建appsettings文件时,不要组合在一起.我想回到旧版本,我在csproj文件中放了一些东西,但我不知道如何在.net核心中执行此操作,我在属性或配置中没有看到任何内容

在此输入图像描述

Gen*_*Mac 30

在解决方案的项目文件中,您可以编辑或添加<ItemGroup>元素中的<Project>元素.这对我有用:

  <ItemGroup>
    <Content Include="appsettings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
    <Content Include="appsettings.Development.json;appsettings.Local.json;appsettings.Production.json;">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <DependentUpon>appsettings.json</DependentUpon>
    </Content>
  </ItemGroup>
Run Code Online (Sandbox Code Playgroud)

请注意我的控制台项目目标是.Net Core 2.0,并且在Visual Studio Pro 2017版本15.7.5中运行.此外,如果您的解决方案资源管理器没有立即刷新,请尝试卸载并重新加载项目.

  • 另外,fwiw,事实证明您不需要单独声明父级。这里两个“Content”节点中的第一个是不必要的。只需将它们全部列在“Include”属性中并设置“DependentUpon”即可。 (2认同)

dan*_*l89 22

在此输入图像描述

您只需单击“文件嵌套”图标,然后选择“Web”


Yah*_*ous 9

按照建议使用<ItemGroup>with给了我一个关于“包含重复的‘内容’项”的错误(在 Visual Studio 2019 中)。事实证明,.NET SDK 默认包含项目目录中的“内容”项。将属性设置为似乎有点僵化,所以现在我将项目包含为.<Content>EnableDefaultContentItemsfalse<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>
Run Code Online (Sandbox Code Playgroud)

这确实显示了分组的文件,但在解决方案资源管理器中显示了其构建操作“无”和“不复制”的属性,所以我想这就是希望它们分组的代价?

使用构建操作“无”和“不复制”对 appsettings.staging.json 进行分组

FWIW: appsettings-json-not-in-hierarchy中建议的文件嵌套规则 不会将文件显示为分组/嵌套,但如果按下解决方案资源管理器折叠按钮,则会使其折叠。

  • 在 VS 2019 中,您只需单击解决方案资源管理器顶部的“文件嵌套”按钮。https://learn.microsoft.com/en-us/visualstudio/ide/file-nesting-solution-explorer?view=vs-2019 (8认同)