0

I am trying to package in a nuget a folder and files structure so they will be copied to the target project. That is, only content. I want all these files and folders to be copied when the nuget is installed.

This is my nuspec:

<?xml version="1.0"?>
<package>
  <metadata>
    <id>MVCViewProvider</id>
    <version>1.0.0</version>
    <authors>M</authors>
    <owners>M</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Test Package</description>
    <releaseNotes>Test Package</releaseNotes>
    <copyright>Copyright 2018</copyright>
    <tags>Tag1 Tag2</tags>
  </metadata>
  <files>
    <file src="Controllers\**\*.*" target="Content\Controllers" />
    <file src="Views\**\*.*" target="Content\Views" />
  </files>
</package>

When I execute this command in PowerShell:

nuget pack MVCViewProvider.nuspec

I get a package with this content:

enter image description here

My folders are Views and Controllers. The problem is that when I add the nuget to other project nothing is created.

What am I doing wrong?

Alpha75
  • 2,140
  • 1
  • 26
  • 49
  • 2
    The source files would need to be added to a target folder of 'Content'. Also note that this will only work for projects that use a packages.config file - https://learn.microsoft.com/en-gb/nuget/create-packages/creating-a-package – Matt Ward Feb 05 '18 at 14:08
  • I've created a 'Content' folder and didn't work. My target projects doesn't use packages.config. They use PackageReference in csproj. Can't I add files to my source code if I don't work with packages.config file? I edited my nuspec xml file. – Alpha75 Feb 06 '18 at 08:20

1 Answers1

3

What am I doing wrong?

The package needs to be changed to use contentFiles. That because NuGet packages that work with Packages.config, don't always work in transitive NuGet environments (projects using Project.json or PackageReferences). Packages that work in transitive NuGet environments must use "contentFiles" instead of "content".

For example, I have created a contentFiles folder, which including sub folder any\any\Controllers and any\any\Views. Add Test.cs in the folder Controllers and add Test.cshtml in the Views folder:

enter image description here

Then create a .nuspec under the folder TestNuGetSample where the contentFiles folder is located:

enter image description here

Following is my .nuspec scripts:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
  <metadata>
    <id>MyTestCore</id>
    <version>4.0.0</version>
    <authors>TestContentFile</authors>
    <owners>TestContentFile</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Package Description</description>
    <contentFiles>
      <files include="any/any/Views/Test.cshtml" buildAction="content" flatten="true" copyToOutput="false"/>
      <files include="any/any/Controllers/Test.cs" buildAction="content" flatten="true" copyToOutput="false"/>
    </contentFiles>
  </metadata>

  <files>
    <file src="contentFiles/any/any/Views/Test.cshtml" target="contentFiles/any/any/Views/Test.cshtml" />
    <file src="contentFiles/any/any/Controllers/Test.cs" target="contentFiles/any/any/Controllers/Test.cs" />
  </files>
</package>

Then pack this .nupsec file to nuget package, and set it to the nuget local repository.

Create a new test project ASP.NET Core MVC project with PackageReference in csproj, then install that nuget package, Test.cs and Test.cshtml will be added to the new project:

enter image description here

Hope this helps.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135