Skip to content

[release/10.0-preview7] Support all subscribers to OnNotFound event #62835

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

Merged

Conversation

github-actions[bot]
Copy link
Contributor

@github-actions github-actions bot commented Jul 21, 2025

Backport of #62798 to release/10.0-preview7

/cc @ilonatommy

Support all subscribers to OnNotFound event

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.

Customer Impact

The change gives more flexibility to the user:

  • 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
};
  • 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.

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

It's not breaking existing scenarios, it's adding a new way of defining not found content.

Verification

  • Manual (required)
  • Automated

@github-actions github-actions bot requested a review from a team as a code owner July 21, 2025 07:27
@ilonatommy ilonatommy requested a review from lewing July 21, 2025 07:32
@ilonatommy ilonatommy self-assigned this Jul 21, 2025
@ilonatommy ilonatommy added the area-blazor Includes: Blazor, Razor Components label Jul 21, 2025
@ilonatommy ilonatommy added this to the 10.0-preview7 milestone Jul 21, 2025
@ilonatommy ilonatommy added the Servicing-consider Shiproom approval is required for the issue label Jul 21, 2025
@ilonatommy ilonatommy requested a review from oroztocil July 22, 2025 09:13
@ilonatommy ilonatommy added Servicing-approved Shiproom has approved the issue and removed Servicing-consider Shiproom approval is required for the issue labels Jul 23, 2025
Copy link
Contributor

Hi @@github-actions[bot]. This PR was just approved to be included in the upcoming servicing release. Somebody from the @dotnet/aspnet-build team will get it merged when the branches are open. Until then, please make sure all the CI checks pass and the PR is reviewed.

@danmoseley danmoseley merged commit 0e4658c into release/10.0-preview7 Jul 23, 2025
29 checks passed
@danmoseley danmoseley deleted the backport/pr-62798-to-release/10.0-preview7 branch July 23, 2025 17:31
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 Servicing-approved Shiproom has approved the issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants