Skip to content

API proposal for Router.NotFoundPage #62409

@ilonatommy

Description

@ilonatommy
Member

Background and Motivation

Blazor’s Router component determines which page to display based on the current route. Previously, customizing the content shown for unmatched routes (i.e., 404 “not found” pages) required using the NotFound render fragment, which could be limiting in scenarios where you want to display a dedicated component or page type or unify the not-found experience for non-blazor pages re-execution.

This proposal introduces a new NotFoundPage property on Microsoft.AspNetCore.Components.Routing.Router, allowing developers to specify a component type to render when no route matches. This provides a more flexible and reusable way to handle “not found” scenarios in Blazor applications.

Proposed API

namespace Microsoft.AspNetCore.Components.Routing
{
    public class Router : IComponent
    {
+       public Type? NotFoundPage { get; set; }
    }
}

Usage Examples

<Router AppAssembly="@typeof(Program).Assembly"
        NotFoundPage="@typeof(MyCustomNotFoundComponent)">
</Router>

Where MyCustomNotFoundComponent is a Blazor component with a route that you define, for example:

@path "/my-not-found"
<h1>Page Not Found</h1>
<p>The page you are looking for does not exist.</p>

Alternative Designs

  • The existing NotFound render fragment allows inline markup for unmatched routes, but does not support specifying a reusable component type and does not render for corner cases, like started response in streaming rendering.
  • Other frameworks often allow specifying a “not found” page by type or path, which this proposal aligns with.
  • An alternative could have been to enhance the NotFound fragment to accept a component type, but a dedicated property is more explicit and discoverable.

Risks

  • If both NotFound and NotFoundPage are set, there may be ambiguity about which takes precedence. NotFoundPage does. Documentation should clarify the behavior.
  • If the specified NotFoundPage type is not a valid Blazor component or is a component without RouteAttribute, a runtime error will occur.

Activity

added
area-blazorIncludes: Blazor, Razor Components
api-suggestionEarly API idea and discussion, it is NOT ready for implementation
on Jun 19, 2025
added this to the 10.0-preview7 milestone on Jun 19, 2025
added
api-ready-for-reviewAPI is ready for formal API review - https://github.com/dotnet/apireviews
and removed
api-suggestionEarly API idea and discussion, it is NOT ready for implementation
on Jun 23, 2025
dotnet-policy-service

dotnet-policy-service commented on Jun 23, 2025

@dotnet-policy-service
Contributor

Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:

  • The PR contains changes to the reference-assembly that describe the API change. Or, you have included a snippet of reference-assembly-style code that illustrates the API change.
  • The PR describes the impact to users, both positive (useful new APIs) and negative (breaking changes).
  • Someone is assigned to "champion" this change in the meeting, and they understand the impact and design of the change.
javiercn

javiercn commented on Jul 7, 2025

@javiercn
Member

Consider making it nullable

halter73

halter73 commented on Jul 7, 2025

@halter73
Member

API Review Notes:

  • Is NotFoundPage nullable?
    • Yes.
  • Should it be an error if both NotFoundPage and the NotFound child content are defined?
    • Maybe we should make it a warning instead if we're concerned about breaking.
  • Should we deprecate the NotFound render fragment?
    • Possibly
  • Could we make it possible to specify the NotFoundPage inside the NotFound render fragment?
    • This would be a non-trivial change.

API Approved!

namespace Microsoft.AspNetCore.Components.Routing
{
    public class Router : IComponent
    {
+       public Type? NotFoundPage { get; set; }
    }
}
javiercn

javiercn commented on Jul 7, 2025

@javiercn
Member

Make an error having render fragment and page at the same time

added
api-approvedAPI was approved in API review, it can be implemented
and removed
api-ready-for-reviewAPI is ready for formal API review - https://github.com/dotnet/apireviews
on Jul 7, 2025
self-assigned this
on Jul 11, 2025
added
api-ready-for-reviewAPI is ready for formal API review - https://github.com/dotnet/apireviews
and removed
api-approvedAPI was approved in API review, it can be implemented
on Jul 11, 2025
dotnet-policy-service

dotnet-policy-service commented on Jul 11, 2025

@dotnet-policy-service
Contributor

Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:

  • The PR contains changes to the reference-assembly that describe the API change. Or, you have included a snippet of reference-assembly-style code that illustrates the API change.
  • The PR describes the impact to users, both positive (useful new APIs) and negative (breaking changes).
  • Someone is assigned to "champion" this change in the meeting, and they understand the impact and design of the change.
ilonatommy

ilonatommy commented on Jul 11, 2025

@ilonatommy
MemberAuthor

Changing labels, so that we won't forget to revisit it on the next meeting. Information needed:
do we want to throw or do we want to warn about both NotFound fragment and NotFoundPage set?

javiercn

javiercn commented on Jul 14, 2025

@javiercn
Member

We decided that we are throwing

added
api-approvedAPI was approved in API review, it can be implemented
and removed
api-ready-for-reviewAPI is ready for formal API review - https://github.com/dotnet/apireviews
on Jul 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

api-approvedAPI was approved in API review, it can be implementedarea-blazorIncludes: Blazor, Razor Components

Type

No type

Projects

No projects

Relationships

None yet

    Participants

    @halter73@javiercn@ilonatommy

    Issue actions

      API proposal for `Router.NotFoundPage` · Issue #62409 · dotnet/aspnetcore