本文介绍了安装该软件包时,我的nuget软件包中的content文件夹未将文件添加到根目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am running .net core 1.1 and nuget 3.5.

这是我的秘密:

<?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文件夹中.

As we can see, the appsettings.json file is located in the content folder in the nuget package.

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

However, when I install the package to my project, the appsettings.json file is not copied to the project root.

我想念什么?还是在.net核心中做了不同的事情?

What am I missing? Or is this done differently in .net core?

推荐答案

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

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

Projects using the PackageReference style of NuGet references use the contentFiles section instead which allows to specify the build action to use. Those files aren't copied to the project, but are included as items in the project and optionally set to copy to the consuming project's build output:

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

请参见 contentFiles文档以获取更多详细信息.

See the contentFiles documentation for more detail.

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

Note that those files aren't copied to the project but included logically. Only packages.config projects support this since there was no other way to contribute files - the documentation also states that these files should have been considered immutable anyway.

这篇关于安装该软件包时,我的nuget软件包中的content文件夹未将文件添加到根目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 10:55