Blazor Server : How to access css file(s) of RazorClassLibrary from Blazor ?

Hamed Vaziri 136 Reputation points
2022-02-19T09:09:21.423+00:00

Hi guys ...
In my sample Blazor server project, I'm using dynamic assembly loading, to load my RazorClassLibrary dll file into my main blazor server project. This works correctly but i don't know how to load static files of my razorClassLibrary project such as css.
Where is the problem & how to solve this?
Thanks in advance

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,154 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,383 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,011 Reputation points Microsoft Vendor
    2022-02-21T02:25:58.787+00:00

    Hi @Hamed Vaziri ,

    To access the RCL's static assets from the main project (the Blazor server project), First, place the static assets in the wwwroot folder of the RCL and reference the static assets with the following path in the app: _content/{PACKAGE ID}/{PATH AND FILE NAME}. The {PACKAGE ID} placeholder is the library's package ID. The package ID defaults to the project's assembly name if <PackageId> isn't specified in the project file. The {PATH AND FILE NAME} placeholder is path and file name under wwwroot.

    For example, I create an Asp.net 6 Razor Class Library (RCL) named "RCLComponentsLib", add a Stylesheet1.css file in the wwwroot folder.

    the Stylesheet1.css file:

    image1.png

    Then, in the RCL application, there have a component as below:

    176155-image.png

    In the Blazor application, add the app.UseStaticFiles(); middleware and add the RCL project reference. Then, consume the RCL component content like this:

        <RCLComponentsLib.Component1></RCLComponentsLib.Component1>

    The result like this:

    176197-image.png

    More detail information, see Create an RCL with static assets in the wwwroot folder and Consume content from a referenced RCL.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments

  2. Hamed Vaziri 136 Reputation points
    2022-02-21T07:11:03.927+00:00

    Many thanks @Zhi Lv - MSFT
    But, As i told in my first post, I'm using rcl dynamically and i don't want to add it as reference!
    If you give me a solution which use rcl dynamically.
    Thanks in advance ..


  3. Hamed Vaziri 136 Reputation points
    2022-02-23T14:36:59.163+00:00

    Many thanks @Zhi Lv - MSFT
    Also, after a lot of research on Oqtane framework which can load rcl dynamically, i found that the static css & js files(s) of rcl project copied to Oqtane.Server project static files inside a folder as follow :

    ..\wwwroot\Modules\MyRCLProjectName\Module.css
    ..\wwwroot\Modules\MyRCLProjectName\Module.js

    This copy process occures automatically & immediately after i build my rcl solution. In the MyRCLProjectName.Package project, we have debug & release cmd file(s) which execute this code(s) after i build entire solution :

    ..
    XCOPY "..\Server\wwwroot*" "....\oqtane.framework-dev\Oqtane.Server\wwwroot\" /Y /S /I
    ..

    To use these staic files, In Oqtane.Server and in _Host.cshtml's code behind, It create "link ..." tag dynamically inside head tag.

    That's it!
    However, Thank you very much ..

    0 comments No comments