RCL components not recognised without code behind file

markcooper 96 Reputation points
2021-11-26T11:49:11.147+00:00

I have a razor class library that I am uplifting to .NET6. The RCL is packaged with nuget and consumed by a blazor server application.

In the RCL I have blazor components (.razor files).

When my components don't have code behind files, they aren't recognised by the code at build time (green squiggly line) and aren't loaded at runtime either.

I have tried using the default namespace from the folder path, and also adding the new @namespace directive at the top of the file.

152924-image.png

Is this intended behaviour?

I am using VS2022.

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,375 questions
0 comments No comments
{count} votes

1 additional answer

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,006 Reputation points Microsoft Vendor
    2021-11-29T07:24:39.98+00:00

    Hi @markcooper ,

    When my components don't have code behind files, they aren't recognised by the code at build time (green squiggly line) and aren't loaded at runtime either.
    Is this intended behaviour?

    No, it is not intended behavior. Based on your description, I try to create a new Asp.net 6 Razor class library and the Asp.net 6 Blazor Server application, it works well on my side, no matter the component with or without the background file, I can use the components in the Blazor application.

    For your behavior (the component without code-behind file not recognized), I think the problem may be related to the "Build Action" property of the component page, which is set to "None".

    To solve this issue, you could right click the component and select the properties option, then set the Build Action property to Content:

    153217-image.png

    Then, rebuild the Razor Class Library, and update the Razor class library reference in the Blazor Application.

    The result is like below:

    153245-image.png


    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