Skip to content

Support all subscribers to OnNotFound event #62798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

ilonatommy
Copy link
Member

@ilonatommy ilonatommy commented Jul 18, 2025

External subscribers can set NotFoundEventArgs.Path

Connected with risks listed in #62412. In this PR, the listed scenarios are treated as supported, excluding case "we have no router and the request has not started" where we are not able to render the path set by external subscriber and only re-execution could render the not found contents.

This PR focuses on fixing scenario "we have blazor's router and the request has not started, we allow the Router to render only if it has NotFoundPage parameter passed". It also adds tests for all the scenarios mentioned in the linked issue.

Description

  • Previously Router ignored all event calls if NotFoundPage was not populated. That included a case of external subscriber that set NotFoundEventArgs.Path.
  • We're changing the condition to treat populated args.Path as a request to render the contents in this path. In that case, Router is able to find a component type by route and render it.
  • [Docs] The update adds flexibility: application can either set Router.NotFoundPage or NotFoundEventArgs.Path. If both are defined, we should respect NotFoundEventArgs.Path because it's easier to overwrite it. It can be used, e.g. like this:
NavigationManager.OnNotFound += (sender, args) =>
{
    // Show different not-found pages based on the attempted URL
    if (NavigationManager.BaseUri.StartsWith("/admin/"))
        args.Path = "/admin-not-found";
    else if (NavigationManager.BaseUri.StartsWith("/api/"))
        args.Path = "/api-not-found";
    else if (NavigationManager.BaseUri.StartsWith("/user/"))
        args.Path = "/user-not-found";
    // Otherwise, let the default `NotFoundPage` handle it
};
  • [Docs] For the scenario to work, external subscriber has to subscribe to NavigationManager.OnNotFound before Router does it. In case of custom router, it's enough to do it in its OnParametersSet/Async, in case of default Router, it's best to do it in App.razor.

@ilonatommy ilonatommy added this to the 10.0-rc1 milestone Jul 18, 2025
@ilonatommy ilonatommy requested review from javiercn and oroztocil July 18, 2025 11:13
@ilonatommy ilonatommy self-assigned this Jul 18, 2025
@github-actions github-actions bot added the area-blazor Includes: Blazor, Razor Components label Jul 18, 2025
@ilonatommy ilonatommy marked this pull request as ready for review July 18, 2025 14:41
@ilonatommy ilonatommy requested a review from a team as a code owner July 18, 2025 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy