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

Merged
merged 4 commits into from
Jul 21, 2025

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
@ilonatommy ilonatommy enabled auto-merge (squash) July 21, 2025 06:44
@ilonatommy
Copy link
Member Author

/ba-g failures are connected with the infrastructure, this PR does not update templates, we can skip additional run of template tests

@ilonatommy ilonatommy merged commit b568259 into dotnet:main Jul 21, 2025
29 of 30 checks passed
@ilonatommy
Copy link
Member Author

/backport to release/10.0-preview7

Copy link
Contributor

Started backporting to release/10.0-preview7: https://github.com/dotnet/aspnetcore/actions/runs/16411012784

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.

4 participants