Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

json - The content folder in my nuget package isn't adding the files to the root when installing the package

I am running .net core 1.1 and nuget 3.5.

Here is my 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="binDebug
et462classlibrary1.dll" target="lib
et462classlibrary1.dll" />
    <file src="appsettings.json" target="content
et462appsettings.json" />
  </files>

</package>

It produces this package: enter image description here

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

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The content subdirectory is only used for packages.config projects.

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>

See the contentFiles documentation for more detail.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...