0

I'm trying to use a custom 404 component in a Blazor Server app with this router setup:

<Router AppAssembly="@typeof(App).Assembly" @rendermode="InteractiveServer">
    <Found Context="routeData">
        <RouteView RouteData="@routeData" DefaultLayout="typeof(MainLayout)" />
    </Found>
    <NotFound>
        <LayoutView Layout="typeof(MainLayout)">
            <NotFoundComponent />
        </LayoutView>
    </NotFound>
</Router>

However, when I run the app, I get the following error:

An unhandled exception occurred while processing the request.
InvalidOperationException: Cannot pass RenderFragment<T> parameter 'Found' to component 'Router' with rendermode 'InteractiveServerRenderMode'. Templated content can't be passed across a rendermode boundary, because it is arbitrary code and cannot be serialized.

Microsoft.AspNetCore.Components.Endpoints.SSRRenderModeBoundary.ValidateParameters(IReadOnlyDictionary<string, object> latestParameters)

It seems like the @rendermode="InteractiveServer" is causing the Found template to fail. I want to display a custom 404 component, but it doesn't work with this global render mode.

Question:

How can I use a custom 404 component (NotFoundComponent) in a Blazor Server app without getting the RenderFragment<T> serialization error? Is there a different approach for defining the router or render mode that supports custom templates?

Note: working in .net9.0

2
  • What version are you on ans on what version did you start the project? Commented Aug 22 at 10:58
  • @HenkHolterman, forgot to mention, but I'm working in .net9.0. Commented Aug 22 at 11:08

2 Answers 2

0

For global interactivity, just move the @rendermode="InteractiveServer" from Routes.razor to App.razor, as in <Routes @rendermode="InteractiveServer" />.

This will resolve the RenderFragment error.
But I'm not sure that the <NotFound> part still works in this setup.

See this issue on GitHub.

Sign up to request clarification or add additional context in comments.

5 Comments

The <NotFound> does not work with Routes...
No, it doesn't. I've added a link.
Seems to be fixed in .net10, but is there any way to fix this in .net9?
As the article says, you will need a fallback route. I'm not sure how, maybe take look at a pre-8 project for the details.
Yes, fixed it with app.UseStatusCodePagesWithRedirects("/error-page/{0}");
0

I've fixed with the following:

Added app.UseStatusCodePagesWithRedirects("/error-page/{0}"); to the Program.cs.

Added the page CustomErrorPage.razor with the following content:

@page "/error-page/{StatusCode:int}"

<div>content</div>

@code {
    [Parameter]
    public int StatusCode { get; set; }

    public bool Is404 => StatusCode == 404;
    public string Heading => Is404 ? "Page not found 404" : $"Error {StatusCode}";
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.