NuGet Add Content Files in .NET Core

09 Oct 2020

With NuGet it is possible to package and distribute content files similar to npm package manager. The following steps show how to add and consume a content folder.

For full source code see:

In the (*.csproj) add the following lines of code. It adds MyContent folder to the class library.

  <None Include="MyContent\**" Pack="true" />
  <Content Include="ClassLibrary.targets" PackagePath="build\ClassLibrary.targets" />

Add a new file ClassLibrary.targets. It defines where to extract MyContent folder to in the consuming application.

<Project xmlns="">
        <SourceScriptFiles Include="$(MSBuildThisFileDirectory)..\content\**" />
    <Target Name="CopyScriptsToProject" BeforeTargets="Build">
        <Copy SourceFiles="@(SourceScriptFiles)" DestinationFolder="$(ProjectDir)MyContent\" />

Then create the package by running Pack.

Add content folder to NuGet

How does NuGet internally store files?

To verify falies are packet into NuGet as expected rename any (*.nupkg) file to (* and open it.

Unzip NuGet package

Add and Build Package in Consuming Application

In the consuming application add the (*.nupkg) file and build it. MyContent folder is copied from package to solution.

Consuming application with content folder from NuGet package