.net-core - 作为 dotnet 构建的一部分,如何将 appsettings.json 复制到 DEBUG 文件夹

标签 .net-core visual-studio-code console-application visual-studio-debugging appsettings

我有一个控制台应用程序,它使用 appsettings.json 来保存配置值。

在 Visual Studio IDE 中,当我为 appsettings.json 设置 AlwaysCopy 时,它会被复制到 DEBUG 文件夹,作为 BUILD 的一部分。

但是,在 .net 核心中,当我运行 dotnet build 时,它不会将 appsettings.json 复制到 DEBUG 文件夹。
我已经将以下 xml 配置添加到 .csproj

  <Content Include="appsettings.json">
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
  </Content>

当我这样做时 dotnet publish ,它正在被复制到发布文件夹。但是,当我这样做时不会发生dotnet build .你能告诉我,为什么没有发生,我应该怎么做才能将 appsettings.json 复制到 DEBUG 文件夹?

最佳答案

在另一个线程中找到答案是 Stackoverflow( The difference between build and publish in VS? )。在 .NET Core 中,与 .NET Framework 相比,构建和发布是不同的。

Building .NET Framework applications will generate the same files as Publish. It will create all the dependencies as binaries including external dependencies(NuGets for instance). So the product of dotnet build is ready to be transferred to another machine to run

Building .NET Core applications, if the project has third-party dependencies, such as libraries from NuGet, they're resolved from the NuGet cache and aren't available with the project's built output. Therefore the product of dotnet build isn't ready to be transferred to another machine to run. You need to run Publish to get all 3rd party dependencies as binaries in output folder.



如果我们希望文件成为发布的一部分,我们需要添加以下属性。在构建中,此文件不会复制到 DEBUG/RELEASE 文件夹中。
<Content Include="appsettings.json">
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
  </Content>

关于.net-core - 作为 dotnet 构建的一部分,如何将 appsettings.json 复制到 DEBUG 文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59926105/

相关文章:

entity-framework-core - .Net Core Entity Framework MySQL - 字符串字段仅存储 255 个符号

visual-studio-code - VSCode 在 jsconfig.js 中提示找不到 node_modules/source-map/source-map

c++ cin.getline() 失败

c# - 是否可以以文化不变的方式动态构建字符串?

.net - RabbitMQ .NET 客户端中的奇怪 "No ip address could be resolved"

entity-framework - Entity Framework Core 仅保存子子列表中的一个(最后一个)

visual-studio-code - 如何在 VSCode 中的 Markdown 代码块中添加自定义语言语法荧光笔?

visual-studio-code - 在 Chromebook 上找不到 VSCode 远程 SSH 命令

java - 如何在小程序中运行该程序?

c++ - 如何在c/c++控制台程序中正常输出多字节字符?