asp.net-coreblazorrazor-pages.net-8.0razor-class-library

Referencing a razor page from a Razor Class Library is giving a 404 error


I am trying to reference a razor page contained in a Razor Class Library in a Blazor project. When I go to the route of my page I am getting a 404 error.

Steps to recreate

Create a new solution as a Blazor Web App (.NET 8.0) template. Add a new Razor Class Library project (Support pages and views checked) Edit the Page1.cshtml and have the following at the top:

@page "/test"

Add a reference to the Razor Class Library(RazorClassLibrary1) from the Blazor App (BlazorApp1).

In BlazorApp1 edit Routes.razor to be:

<Router AppAssembly="typeof(Program).Assembly" AdditionalAssemblies="new[]{typeof(RazorClassLibrary1.MyFeature.Pages.Page1Model).Assembly}">
    <Found Context="routeData">
        <RouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)" />
        <FocusOnNavigate RouteData="routeData" Selector="h1" />
    </Found>
</Router>

Edit Program.cs of BlazorApp1 to include:

  app.MapRazorComponents<App>()
      .AddInteractiveServerRenderMode()
      .AddAdditionalAssemblies([typeof(RazorClassLibrary1.MyFeature.Pages.Page1Model).Assembly] );

Start the application and browse to /test and you get a 404. What am I doing wrong?


Solution

  • Why are you trying to use a cshtml defined page? Use a .razor page.

    Here's a Demo project set up as you describe that works.

    https://github.com/ShaunCurtis/SO79028139