【问题标题】:The content folder in my nuget package isn't adding the files to the root when installing the package安装包时,我的 nuget 包中的内容文件夹未将文件添加到根目录
【发布时间】:2017-11-26 07:29:49
【问题描述】:

我正在运行 .net core 1.1 和 nuget 3.5。

这是我的 nuspec:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>ClassLibrary1</id>
    <version>1.0.0.4</version>
    <title>Class Library Test Content</title>
    <authors>name</authors>
    <owners>name</owners>        
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>package to test config file transfer</description>
    <copyright>Copyright 2017</copyright>
    <tags>TagUno TagDos</tags>
    <contentFiles>
      <files include="appsettings.json" buildAction="Content" copyToOutput="true" />
    </contentFiles>
  </metadata>

  <files>
    <file src="bin\Debug\net462\classlibrary1.dll" target="lib\net462\classlibrary1.dll" />
    <file src="appsettings.json" target="content\net462\appsettings.json" />
  </files>

</package>

它产生这个包:

我们可以看到,appsettings.json 文件位于 nuget 包的 content 文件夹中。

但是,当我将包安装到我的项目时,appsettings.json 文件不会复制到项目根目录。

我错过了什么?还是在 .net core 中这样做的方式不同?

【问题讨论】:

    标签: json asp.net-core nuget .net-core nuget-package


    【解决方案1】:

    content 子目录仅用于packages.config 项目。

    使用PackageReference 样式的 NuGet 引用的项目使用contentFiles 部分代替,它允许指定要使用的构建操作。这些文件不会复制到项目中,而是作为项目包含在项目中,并且可以选择设置为复制到使用项目的构建输出中:

    <package>
      <metadata>
        ...
        <contentFiles>
            <files include="appsettings.json" buildAction="Content" copyToOutput="true" />
        </contentFiles>
      </metadata>
      <files>...</files>
    </package>
    

    请参阅contentFiles documentation for more detail

    请注意,这些文件并未复制到项目中,而是在逻辑上包含在内。只有 packages.config 项目支持这一点,因为没有其他方法可以贡献文件 - 文档还指出这些文件无论如何都应该被认为是不可变的。

    【讨论】:

    • 这没有成功将文件传输到项目根目录。我已更新我的 nuspec 文件以反映更改以供审核。
    • 不支持将文件复制到项目中。这是已被内容文件替换的“旧”nuget 的一项功能。
    • 顺便说一句,通过 nuget 内容文件发送“默认配置”的做法无论如何都被认为是一种不好的做法,因此不鼓励这样做。 (内容文件应该是不可变的)
    • “逻辑包含”是什么意思?
    • 我也有同样的问题。我有一个包含默认 appsettings 的库。此默认应用设置已正确打包在我的库中。在我的消费项目中的路径“contentFiles/any/any/appsettings.mydefaults.json”中也可以正确看到它。那么,调用AddJsonFile(path)时应该使用什么路径呢?我尝试了类似contentFiles/any/any/appsettings.mydefaults.json 的东西,但它抛出了一个异常说The configuration file 'contentFiles/any/any/appsettings.mydefaults.json' was not found and is not optional.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-27
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多