Reputation: 5058
I have razor class library which has wwwroot folder and some static assets inside it (let's call it MyWeb.Reports project because it will be used to create static HTML reports using razor as generator).
I also have asp.net core hosting app (lets call it MyWeb) which uses that dll (references that firstly mentioned project inside Visual Studio-MyWeb.Reports). Class library purpose is to generate static HTML file (it renderes razor components)
I would like to be able to render CSS inside HTML as inline string because HTML file has to be standaolone (no additional requests to any server once html is rendered).
My idea was to use something like this File.ReadAllText("~/styles.css") inside razor component. That would work if styles.css was inside wwwroot folder of asp.net core web app.
It feels strange to have css for component library inside web app, and components inside class library.
Keep in mind that I am not creating web app here (there is no components inside web app project). It's porpuse is just to call Web.Reports.dll which will generate HTML and MyWeb will just have controller which will return generated string(stream).
Is it possible to copy wwwroot from referenced project into one that will actually be hosted?
Upvotes: 3
Views: 3360
Reputation: 308
This article https://dotnetstories.com/blog/How-to-Include-static-files-in-a-Razor-library-en-7156675136 describes how to access files from Razor class library.
In your razor class library csproj:
Example:
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="5.0.0-rc.2.*" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="5.0.0-rc.2.*" PrivateAssets="all" />
<PackageReference Include="System.Net.Http.Json" Version="5.0.0-rc.2.*" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="5.0.0-rc.2.*" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="wwwroot\**\*" />
</ItemGroup>
Then you can do something like:
var assembly = typeof(Library.Class1).Assembly;
var filePaths = assembly.GetManifestResourceNames().Where(rnn => rnn.Contains("wwwroot"));
foreach (var path in filePaths)
{
Console.WriteLine(path);
}
Upvotes: 1