Blazor Web App using the Razor pages (.razor) from RCL

Balu Raju 81 Reputation points
2025-07-28T12:35:51.12+00:00

Hello,

I'm trying to have the Razor Class Library (RCL) to serve the Razor pages (.razor) into Blazor Web App.

First, I created a RCL project called RazorClassLibrary and simply added a new Razor compnent (.razor page). The page is under a folder called Pages. the page is simple as below:
@page "/TestPage"

<h3>TestPage</h3>

@code {

}

then I created a new Blazor Web App that generated the template code the same solution

Added a reference to the RCL project that I created in the above step.

I added the new menu item in blazor web App by modifying the code in NavMenu.razor to href the /TestPage from RCL.
<div class="nav-item px-3">

 <NavLink class="nav-link" href="/TestPage">

     <span class="bi bi-list-nested-nav-menu" aria-hidden="true"></span> Test Page From RCL

 </NavLink>

</div>

I also modified the Routes.razor as below:
<Router AppAssembly="@typeof(Program).Assembly"

    AdditionalAssemblies="new[] { typeof(RazorClassLibrary.Pages.TestPage).Assembly }">

Nothing was done in App.razor

My Program.cs in Balzor web app has the code below that is unchanged from template generate code

using BlazorWebApp.Components;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddRazorComponents()

.AddInteractiveServerComponents();

var app = builder.Build();

// Configure the HTTP request pipeline.

if (!app.Environment.IsDevelopment())

{

app.UseExceptionHandler("/Error", createScopeForErrors: true);

// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.

app.UseHsts();

}

app.UseHttpsRedirection();

app.UseStaticFiles();

app.UseAntiforgery();

app.MapRazorComponents<App>()

.AddInteractiveServerRenderMode();

app.Run();

After compiling and running it in IDE Localhost, I get 404 error
what am I missing?

Developer technologies | .NET | Blazor
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 79,101 Reputation points Volunteer Moderator
    2025-07-28T14:58:50.2+00:00

    While the server supports the RCL endpoint, the Blazor app local router does not. when the Blazor app is running, it will intercept the url, and try to find a Blazor endpoint. To call the razor page, add a route to the Blazor app, then navigate to the fully qualified url. note: this will unload the blazor app and navigate to the RCL page.


  2. Balu Raju 81 Reputation points
    2025-08-01T13:00:10.87+00:00

    I think I found the answer in stackoverflow by Jeff Hill. Thanks to them.
    here is the link
    https://stackoverflow.com/questions/59371239/blazor-razor-class-library-reuse-full-blazor-page

    As suggested in the post

    I modified the app.MapRazorComponents in the Program.cs to chain the AddAdditionalAssemblies
    app.MapRazorComponents<App>()

    .AddInteractiveServerRenderMode()
    
    .AddAdditionalAssemblies(typeof(My.Components.Pages.MyPage).Assembly);
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.