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: github.com/WorkplaceX/Util/tree/master/NuGet

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

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

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

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
        <SourceScriptFiles Include="$(MSBuildThisFileDirectory)..\content\**" />
    </ItemGroup>
    <Target Name="CopyScriptsToProject" BeforeTargets="Build">
        <Copy SourceFiles="@(SourceScriptFiles)" DestinationFolder="$(ProjectDir)MyContent\" />
    </Target>
</Project>

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 (*.nupkg.zip) 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