[release/8.0] [Blazor] Prevent error boundaries from handling exceptions of type NavigationException
#53968
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Prevent error boundaries from handling exceptions of type
NavigationException
Backport of #53826
Fixes an issue where error boundaries may handle
NavigationException
instances and treat them as uncaught errors rather than signals to redirect.Description
The implementation of
NavigationManager.NavigateTo()
for static server rendering (SSR) always throws aNavigationException
. This exception does not communicate an error but acts as a message to be caught by the framework so a redirect can be initiated.Blazor also provides an
ErrorBoundary
component (unrelated to the above) that catches exceptions thrown from within child components, so that errors disable only part of the UI rather than crash the entire page. However, before this PR, theErrorBoundary
component did not account for the fact that exceptions of typeNavigationException
are not critical errors and need to bubble all the way up to be handled by the framework. As a result, error boundaries would prevent child components from being able to initiate redirects during static server rendering.This PR updates Blazor's renderer to exclude
NavigationException
instances from being reported to error boundaries.Fixes #52777
Customer Impact
We've received multiple reports of customers getting confused by this exception without knowing that its existence is what enables navigation during static server rendering. When an
ErrorBoundary
catches and displays this exception, many customers are left to believe that theNavigationException
is being thrown as a result of navigation not succeeding. Ugly workarounds may exist, like restructuring app logic to ensure thatNavigationManager
APIs are not used within error boundaries, but something as fundamental as navigation is expected to work without this type of issue.Regression?
This bug has existed since the ability to redirect from within a component was introduced in .NET 8.
Risk
The fix works by adding a special case for
NavigationException
without changing other functionality, so it's unlikely that a regression will be introduced here.Verification
Packaging changes reviewed?