使用nuget包向appsettings.json添加配置

ruf*_*fen 7 .net configuration nuget-package azure-devops asp.net-core

我已经创建了一个nuget包,我需要在appsettings.json中添加一个部分,或者添加我自己的复制到应用程序中的配置文件,但是我无法弄清楚如何.

I want this: 
{
 "mysection" : 
  { 
        "value1": "value, 
        "value2": "value"
  }
}
Run Code Online (Sandbox Code Playgroud)

要添加到配置文件中,或者在下载nuget包时包含要复制的文件.我们正在使用visual studio teamservices来构建和托管nuget包.

dro*_*der 1

您想要编辑 nuspec 文件并使用 files 元素并在其中添加文件。

<files>
    <file src="myConfig.json" target="Content" />
</files>
Run Code Online (Sandbox Code Playgroud)

如果您使用 NuGet 3.3+ 或 PackageReference 与 NuGet 4+,则应改用 contentFiles elemenet。

<contentFiles>
    <files include="any/any/myConfig.json" buildAction="None" copyToOutput="true" flatten="true" />
</contentFiles>
Run Code Online (Sandbox Code Playgroud)

文档建议指定这两个元素以实现最大兼容性。你可以在这里阅读更多

  • 我认为这更多的是如何引入附加文件,但作者正在寻找的是如何将该设置部分合并到 applicationsettings.json 文件中...类似于如何通过转换添加部分网络配置。 (4认同)