C# VS2022 Blazor webassembly razor components library project does not even compile

Socrates Anibal Rivera Rivera 46 Reputation points
2022-04-02T23:31:38.24+00:00

189457-image.png

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,313 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,461 questions
{count} votes

Accepted answer
  1. Lohith GN 511 Reputation points
    2022-04-04T02:43:22.99+00:00

    Hi @Socrates Anibal Rivera Rivera .

    It can be done. I just created your scenario using Visual Studio 2022 Latest Preview. I chose .NET 7.0 (Preview) as the framework. Steps I did was:

    • Created a new Blazor WebAssembly project
    • Select solution, right click , Add New Project
    • Select Razor Class Library. i left default name that is given to the project - in my case it was RazorClassLibrary1
    • next add project reference of razor class library in blazor project
    • open imports.razor, add namespace of razor class library - in my case it was @using RazorClassLibrary1
    • use the component in index.razor

    And it works as expected.

    Can you explain how you created blazor project & razor class library - all the choices that you made?

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,056 Reputation points Microsoft Vendor
    2022-04-05T02:03:05.977+00:00

    Hi @Socrates Anibal Rivera Rivera ,

    https://drive.google.com/file/d/1r3p4dlGJ_ZRAr-OylMlNEeQN2DQcwTXR/view?usp=sharing

    The Razor class library (with support for pages and views) are not working with the Blazor Webassembly client application, because to use the RCL class library, it need to install the Microsoft.AspNetCore.Mvc framework, and this framework can't install in the Blazor Webassembly client application.

    So, if you want to add Razor component in the Razor class library(RCL), please uncheck the "Support Pages and Views" option when create it, then you can add and use the RCL in the Blazor Webassembly **Client project**.

    If you want to add Razor page in the RCL, you can check the "Support Pages and Views" option when creates it, then you can add the RCL in the Blazor Webassembly **Server project**. Then you can access the razor page via the razor page route.


    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

    2 people found this answer helpful.
    0 comments No comments

  2. Socrates Anibal Rivera Rivera 46 Reputation points
    2022-04-04T17:16:07.117+00:00

    Hello @Lohith GN .
    Thanks for the quick answer.
    I made a video explaining what I did.
    I hope you can see it (the only thing I noticed is that you use net 7 and corresponding ..).
    https://drive.google.com/file/d/1r3p4dlGJ_ZRAr-OylMlNEeQN2DQcwTXR/view?usp=sharing
    Thanks again.
    Regards,
    Sócrates A Rivera R