aspnetcore: Static files not working on Blazor apps using Razor Class Library (RCL)

Describe the bug

Static resources in the “wwwroot” folder of a Razor class library RCL, aren’t deployed correctly. This is causing apps to crash due to the JavaScript interop to throw exception because js files aren’t in place.

To Reproduce

Steps to reproduce the behavior:

  1. Create a Blazor application (Any server-side or client-side)
  2. Create a new RCL project.
  3. Add a reference from the app to the created library
  4. Add the <RazorClassLibrary1.Component1/> tag to Index.razor.
  5. Run the app.

Expected behavior

It should render the component with the “background.png” included in the css style of the library.

Screenshots

image The background of the component doesn’t appear.

Additional context

Using the latest preview 8

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Components" Version="3.0.0-preview8.19405.7" />
    <PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.0.0-preview8.19405.7" />
  </ItemGroup>

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 27 (11 by maintainers)

Most upvoted comments

@arivoir A project reference is no longer sufficient. You also need to add script tags, link tags, etc for the static asset. For example:

<script src="_content/MyLibrary/foo.js"></script>

I can second this issue, following Microsoft documentation does not provide a successful path to functionality for the RCL.

Steps taken to reproduce and result are the same as @arivoir outlined above.

These aren’t auto included in your Blazor application.

From the documentation https://docs.microsoft.com/en-us/aspnet/core/blazor/class-libraries?view=aspnetcore-3.0&tabs=visual-studio

An RCL can include static assets. The static assets are available to any app that consumes the library. For more information, see Reusable Razor UI in class libraries with ASP.NET Core.

And the other documentation page https://docs.microsoft.com/en-us/aspnet/core/razor-pages/ui-class?view=aspnetcore-3.0&tabs=visual-studio

When packing an RCL, all companion assets in the wwwroot folder are included in the package automatically and are made available to apps referencing the package.

Do you mean this isn’t true?

did you include the css and scripts from the Razor Class Library in your main application?

Yes, I tried copying the files manually in the app and adding the references in _Host.cshtml. It works in server-side Blazor, but not in client-side Blazor.