3

I have build NuGet package consisting of Razor Class Library (RCL) project at .net core 2.2. Once packed I see that it contains static assets that I have configured:

package details - sample asset

When I install package inside target project, assets refs are visible inside project structure: enter image description here

But when I try to access them at runtime - i get 404: https://localhost:44312/localizer-assets/lib/vue/vue.min.js

I see that those files are not "physically" placed inside wwwroot folder (they are iniside package folder).

Is there any way to serve those files? How this should be configured?

So far I have been using those approches:

https://www.learnrazorpages.com/advanced/razor-class-library

Can Razor Class Library pack static files (js, css etc) too?

They work fine when I directly reference RCL project, but when packed with NuGet and installed does not work any more.

IT Man
  • 933
  • 3
  • 12
  • 33

1 Answers1

3

They work fine when I directly reference RCL project, but when packed with NuGet and installed does not work any more.

As far as l know, when you use nuget and choose PackageReference nuget management foramt, the files which targets to import into the main project are introduced into your main project as links(The corresponding files will not exist in the physical path of your solution , but will be linked to the corresponding file address in the %userprofile%\.nuget\xxx). It is the feature of PackageReference nuget management format.

Solution

To solve it, l think you can add a custom build target in the nuget project(Include MSBuild props and targets in a package). With it, you can copy the files from nuget package into the main project.

1.add a target file like .targets or .props(must be the same as the package id) into the \build folder(must be created under the project root directory) of the project. All of these are based on nuget packaging mechanism.

2.add these codes into the target file

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
        <SourceScriptFiles Include="$(MSBuildThisFileDirectory)..\content\xxxx(relative paths in the current project)" />
    </ItemGroup>
    <Target Name="CopyScriptsToProject" BeforeTargets="Build">
        <Copy SourceFiles="@(SourceScriptFiles)" DestinationFolder="$(ProjectDir)\wwwroot\xxxx\"
        />
    </Target>
</Project>

Use a target to copy the file @(SourceScriptFiles) into the main project.(The DestinationFolder is just the destination address).

3.If you use nuspec file to pack your package. You should also add these files under the files node in it.

<files>
        <!-- Include everything in \build -->
        <file src="build\**" target="build" />

        <!-- Other files -->
        <!-- ... -->
</files>

In addition, such operation is a pre-build event and when you install the package, you should build your project first and then you will find the file under the destination folder.

Besides, here is a good sample in the github and l hope it can give detailed information and steps. Hope it could help you.

LoLance
  • 25,666
  • 1
  • 39
  • 73
  • Great detailed anwer! I will check this solution and give feedback. I have 2 more questions: as far as i understand files that are already inside target project will be overwritten? What about package deinstalation - as far as i understand copied files will not be deleted during instalation? – IT Man Nov 28 '19 at 07:59
  • Yes. The [Copy task](https://learn.microsoft.com/en-us/visualstudio/msbuild/copy-task?view=vs-2019) will always overwrite the same file.So when you want to save the old file, please try to modify the destination address to save the file in a new folder. And when you uninstall the package, it will not uninstall the copied files. So you would better distinguish the destination address from existing folders in the project, or to create a different folder for later deletion. – LoLance Nov 28 '19 at 10:55
  • Thanks for the answer. One more question, is there any mechanism to force files deletion during package uninstall, if so is it possible to prompt user if to delete? – IT Man Nov 28 '19 at 11:13
  • As I know it's possible. I know there exists something like install.ps1 and uninstall.ps1. Adding these two scripts when creating nuget packages, then install.ps1 will execute when someone install the package, and uninstall.ps1 will execute when someone uninstall that... So I think the uninstall.ps1 is what you're looking for... – LoLance Nov 28 '19 at 11:21
  • I can't find any documents about this now, but you may get some help from [one](https://www.codeproject.com/Articles/1084142/NuGet-for-Source-Using-Add-As-Link-Part), [two](https://stackoverflow.com/questions/38707082/nuget-update-calls-uninstall-ps1-and-then-install-ps1)... – LoLance Nov 28 '19 at 11:23
  • Thanks for all of answers! – IT Man Nov 28 '19 at 12:04
  • @ITMan Glad to know it makes some help. If you meet any issue when trying my solution in my answer, feel free to let me know :) – LoLance Nov 28 '19 at 12:30
  • Great thanks, it will take me some time to verify this concept but definitely will come back with feedback. – IT Man Nov 28 '19 at 13:51