Aspnetcore.docs: New doc: use static assets that are part of a RCL project

Created on 3 Jun 2018  Â·  14Comments  Â·  Source: dotnet/AspNetCore.Docs

I created a Razor Class Library this weekend. I was able to encapsulate Razor Views and associated Models + a Controller in a Razor Class Library and accompanying NuGet package. However, I would also like to encapsulate a collection of files under a Razor Class Library "wwwroot" folder that would be copied over to the "wwwroot" folder of the referencing ASP.NET Core web application upon package installation. Is this possible?


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

P1 P2 PU Source - Docs.ms

Most helpful comment

RPL.Demo
This is my demo to use wwwroot as a part of a RCL project.
I hope it will help someone who are looking for this.

All 14 comments

We have an item tracking experiences around static class libraries + Razor Sdk here - https://github.com/aspnet/Razor/issues/2322. In the meanwhile, our recommendation is to follow what's done in IdentityUI. I'll highlight the relevant pieces of code to use the ManifestEmbeddedFileProvider to share static assets:

@pranavkm Would you recommend using Identity UI as an example for making a RCL with MVC instead of Razor Pages? I have a very similar need with static assets so this is super interesting.

IdentityUI uses Razor Pages - https://github.com/aspnet/Identity/tree/dev/src/UI/Areas/Identity/Pages. That said, Controllers with Views and Razor Pages are two flavors of MVC the framework. We think Razor Pages has fewer ceremony for browser based applications, but either (or both) should be fine to share UI in class libraries.

RPL.Demo
This is my demo to use wwwroot as a part of a RCL project.
I hope it will help someone who are looking for this.

@pranavkm : Your links are broken, can you update them please?

A critical detail that wasn't obvious to me initially is the following package reference:

<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="2.1.0" />

Without it, the application will compile but fail at runtime with:

Could not load the embedded file manifest 'Microsoft.Extensions.FileProviders.Embedded.Manifest.xml' for assembly 'RPL.Demo'

when running:

var filesProvider = new ManifestEmbeddedFileProvider(GetType().Assembly, basePath);

Thanks @sheng-jie for the detailed walkthrough!

@pranavkm : Your links are broken, can you update them please?

I fixed the links but the file numbers are off. @pranavkm can you edit the line numbers?

The first answer explains how to do this. It's work I just try: https://stackoverflow.com/questions/51610513/can-razor-class-library-pack-static-files-js-css-etc-too

This doesn't work if there multiple resourceFiles for different languages. (Resources.resx and Resources.en.resx)
If these files in the project available and the solution of the stackoverflow thread applied the following error occurs:
Error MSB4018 The "GenerateEmbeddedResourcesManifest" task failed unexpectedly. System.InvalidOperationException: An item with the name '' already exists. at Microsoft.Extensions.FileProviders.Embedded.Manifest.Task.Internal.Entry.AddChild(Entry child) at Microsoft.Extensions.FileProviders.Embedded.Manifest.Task.Manifest.AddElement(String originalPath, String assemblyResourceName) at Microsoft.Extensions.FileProviders.Embedded.Manifest.Task.GenerateEmbeddedResourcesManifest.BuildManifest(EmbeddedItem[] processedItems) at Microsoft.Extensions.FileProviders.Embedded.Manifest.Task.GenerateEmbeddedResourcesManifest.Execute() at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext() RCLProject C:\Users\developer\.nuget\packages\microsoft.extensions.fileproviders.embedded\2.2.0\build\netstandard2.0\Microsoft.Extensions.FileProviders.Embedded.targets 65

@pranavkm : Your links are broken, can you update them please?

I fixed the links but the file numbers are off. @pranavkm can you edit the line numbers and review new comments?

🆙 📅 the links

@mttkrb See this issue for workaround:

https://github.com/aspnet/Mvc/issues/8121#issuecomment-410327701

Was this page helpful?
0 / 5 - 0 ratings