How to copy specific folder from a Razor Class Library to the destination via Nuget?

226 views Asked by At

I made a Razor class library project as following in .NET 5.0

enter image description here

I want to make a NuGet package with the Areas folder inside it

enter image description here

So that whenever this package is installed, the whole folder with all its files in the destination will be copied with the same name.

How to pack the whole Areas folder via dotnet pack command?

enter image description here

1

There are 1 answers

1
Mr Qian On BEST ANSWER

Try this:

1) create a <packages_id>.props file under build folder of the project folder. In your side, it should be named as RazorClassLibrary1.props so that the file will work. See this link.

enter image description here

And if your nuget package is called RazorClassLibrary1.1.0.0.nupkg, the file should be RazorClassLibrary1.props and keep the name same as the package id.

2) add these under RazorClassLibrary1.props:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   
        
    <Target Name="CopyFiles" BeforeTargets="Build">
        <ItemGroup>
            <File Include="$(MSBuildThisFileDirectory)..\Areas\**\*.*"></File>
        </ItemGroup>
        
        <Exec Command="xcopy /I/e/d/s/y %(File.Identity) $(ProjectDir)Areas\"></Exec>
            
    </Target>

</Project>

3) add these on RazorClassLibrary1.csproj file:

<ItemGroup>

        <None Include="Areas\**\*.*" Pack="true" PackagePath="Areas"></None>
        <None Include="build\RazorClassLibrary1.props" Pack="true" PackagePath="build"></None>

</ItemGroup>

Also, when you install this new version of nuget package, please clean your nuget caches or delete all files under C:\Users\xxx(current user)\.nuget\packages.

Besides, here is a similar issue.