In ASP.NET Core 10 Blazor, how do Router's NotFoundPage and NavigationManager.NotFound() handle a plant id that does not exist, across render modes?
answer
- a routable component type
- the old fragment is obsolete
- 404 only while rendering on the server
- NotFound() does not return early
- OnNotFound and NotFoundEventArgs.Path
basics
~20 sRouter.NotFoundPage names a routable component shown when nothing matches; a page calls NavigationManager.NotFound() when its data is missing. Under static SSR and prerendering that sets a 404 and renders the page; interactively only global routers show it.
solid answer
~40 sIn .NET 10, `<Router NotFoundPage="typeof(Pages.NotFound)">` replaces the `<NotFound>` fragment, which is now `[Obsolete]`; setting both throws `InvalidOperationException`, and the page type must have an `@page` route. When `/plants/9999` matches the template but the plant is missing, the page calls `Navigation.NotFound()` and then `return`, because the call does not stop the method. Under static SSR it sets status 404 and the router renders the not-found page. With an interactive `Routes` component, the router swaps in the not-found page. With per-page interactivity, it works during prerendering only; afterwards it does nothing, so the page must show its own message. URLs with no endpoint at all are handled by the template's `UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true)`. `NavigationManager.OnNotFound` lets a custom router set `NotFoundEventArgs.Path`.
code
razor · 5 lines@page "/not-found"
@layout MainLayout
<h1>We couldn't find that plant</h1>
<p>It may have sold out or moved. <a href="catalog">Browse the catalog</a>.</p>go deeper
Recall that .NET 10 uses Router.NotFoundPage with a routable NotFound page, and that a page calls NavigationManager.NotFound() when its record is missing.
Explain the difference between an unmatched URL and missing data, why NotFound() needs a return after it, and the template's re-execution line.
Show you know where NotFound() works across static SSR, prerendering, global and per-page interactivity, and how to give users and crawlers a real 404.
Discuss how a team standardises not-found behaviour across render modes, including status codes for search engines and a consistent page.
## Two kinds of not found A Blazor Web App meets missing content in two ways, and they need different tools: 1. **No route.** `/plnts/12` matches no `@page` template. No component runs; the server has no endpoint for the path. 2. **A route, but no data.** `/plants/9999` matches `@page "/plants/{Id:int}"`, the page runs, and the database has no plant 9999. Before .NET 10, Blazor had only the router's `<NotFound>` render fragment for case 1, and no framework call for case 2; static pages set `HttpContext.Response.StatusCode` themselves. ASP.NET Core 10 adds `Router.NotFoundPage`, `NavigationManager.NotFound()` and `NavigationManager.OnNotFound`. ## `Router.NotFoundPage` `NotFoundPage` is a `Type` parameter on the `Router` component in `Routes.razor`: ```razor <Router AppAssembly="typeof(Program).Assembly" NotFoundPage="typeof(Pages.NotFound)"> <Found Context="routeData"> <RouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)" /> </Found> </Router> ``` - The type must be a component **with a route** (`@page "/not-found"`). The router throws an `InvalidOperationException` when it first receives its parameters if the type has no `[Route]`. - The router renders it through the same `Found` template, so it gets the layout. - The old `<NotFound>...</NotFound>` fragment still compiles but is marked `[Obsolete("NotFound is deprecated. Use NotFoundPage instead.")]`. Setting both throws. - The route matters because a page with a URL can also be rendered by status-code re-execution and during streaming rendering, where the fragment could not be used. ## `NavigationManager.NotFound()` A page calls it when its data is missing. What it does depends on how the page is rendering: | Rendering | Effect of `NotFound()` | |---|---| | Static SSR | response status 404 (if it has not started) and the router renders `NotFoundPage` | | Prerendering of an interactive page | the same as static SSR | | Global interactivity (`Routes` is interactive) | the router renders `NotFoundPage`; there is no HTTP response to give a status | | Per-page interactivity, after prerendering | no effect, so the page must show its own message | | Streaming rendering, response already started | the not-found content is rendered by URL, which needs `NotFoundPage` or re-execution to be configured | Two rules come with it: - **It does not end the method.** Write `return;` after it, otherwise the code below (often a `NavigateTo` or a null dereference) still runs. - **It is not the same as a status code.** Setting `HttpContext.Response.StatusCode = 404` does not raise `OnNotFound`, so the router's `NotFoundPage` logic never runs; only status-code re-execution, if configured, can react to that bare 404. ```razor @page "/plants/{Id:int}" @inject NavigationManager Navigation @inject PlantCatalog Catalog @code { [Parameter] public int Id { get; set; } private Plant? plant; protected override async Task OnInitializedAsync() { plant = await Catalog.FindAsync(Id); if (plant is null) { Navigation.NotFound(); return; } } } ``` ## URLs with no route at all For case 1 the .NET 10 Blazor Web App template adds `app.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true)` and a `NotFound.razor` page at `/not-found`. A typed URL with no endpoint gets a 404 from the server, and the middleware re-executes the pipeline for `/not-found`, so the user sees the same page with the 404 status kept. An interactive router behaves consistently: a clicked link to an unknown internal URL is retried as a full page load, so the server answers with the 404 and the re-executed page, while a programmatic navigation to an unknown URL renders `NotFoundPage` directly. ## What users and crawlers see The status code only exists on a server response. That gives three practical rules: - A visitor who opens `/plants/9999` directly gets a real 404 with the not-found page, because the first request is rendered on the server (statically or as a prerender). - A visitor who reaches the same URL by clicking around a globally interactive app sees the same page, but no 404 is sent, because no HTTP request was made for that page. - Search engines request URLs directly, so they see the 404, which keeps deleted plants out of the index. ## Custom routers and `OnNotFound` `NavigationManager.OnNotFound` is an event raised by `NotFound()`. Its `NotFoundEventArgs.Path` can be set by a subscriber to tell the renderer which route to render as not-found content. The built-in `Router` sets it from `NotFoundPage`; an app with its own router subscribes and sets it. ## Checklist for the nursery - One `NotFound.razor` with `@page "/not-found"`, assigned to `NotFoundPage`. - Status-code re-execution to the same path for unknown URLs. - `NotFound(); return;` in detail pages whose record is missing. - An in-page message for per-page interactive components that discover missing data after prerendering.
- A per-page InteractiveServer plant page calls NotFound() from a button handler, and nothing happens. Why?With per-page interactivity, the `Routes` component is not interactive, so no interactive router listens for the call. `NotFound()` only has an effect during prerendering, when the response can still get a 404 and the server router renders the page. After that it is a no-op, so the component has to render its own "not found" message.
- What happens if Routes.razor sets both NotFoundPage and the <NotFound> fragment?The router throws an `InvalidOperationException` saying that setting `NotFound` and `NotFoundPage` together is not supported. The fragment is obsolete in .NET 10, so remove it and keep `NotFoundPage`.
- The <NotFound> fragment was removed in .NET 10 and no longer compiles
- NotFoundPage accepts any component, with or without an @page route
- NavigationManager.NotFound() stops the calling method, like a thrown exception
- Setting HttpContext.Response.StatusCode to 404 raises NavigationManager.OnNotFound
- NotFound() sets a 404 status even after an interactive page has finished prerendering
Assumes ASP.NET Core 10: Router.NotFoundPage, NavigationManager.NotFound() and OnNotFound are new in 10, the <NotFound> render fragment is obsolete, and the Blazor Web App template adds a NotFound.razor page plus status-code re-execution to /not-found.