-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Add NotFound
method in NavigationManager
for interactive and static rendering
#60752
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
Conversation
NotFound
method in NavigationManager
for interactive and static navigation managersNotFound
method in NavigationManager
for interactive and static rendering
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Overview
This PR adds a NotFound mechanism to NavigationManager to support both interactive and static rendering scenarios. Key changes include:
- Introducing new NotFound APIs in NavigationManager (including NotFound, NotFoundCore, NotFoundEvent, and their asynchronous variants) to trigger not found behavior.
- Updating platform-specific implementations (WebAssembly, Server, Http) to override NotFoundCore and properly handle not found responses.
- Adding new tests to verify that both SSR and interactive render modes render the NotFound page or return a 404 status code as expected.
Reviewed Changes
File | Description |
---|---|
src/Components/Components/src/NotFoundRenderingException.cs | Introduces a new exception for handling errors during NotFound rendering. |
src/Components/Components/src/IEndpointHtmlRenderer.cs | Adds the SetNotFoundResponse() API for SSR scenarios. |
src/Components/Components/src/Routing/NotFoundContext.cs | Provides a context for NotFound handling. |
src/Components/Components/src/NavigationManager.cs | Adds new NotFound APIs (event, core methods, async handlers, etc.) for navigation failure scenarios. |
src/Components/WebAssembly/WebAssembly/src/Services/WebAssemblyNavigationManager.cs | Implements NotFoundCore for interactive WebAssembly rendering. |
src/Components/Server/src/Circuits/RemoteNavigationManager.cs | Updates NotFound handling logic for interactive Server rendering. |
src/Components/test/E2ETest/ServerRenderingTests/RenderingTest.cs | Adds tests for verifying NotFound behavior in interactive render modes. |
src/Components/test/E2ETest/ServerRenderingTests/NoInteractivityTest.cs | Adds SSR tests to confirm a 404 status is returned for not found pages. |
src/Components/Components/src/Routing/Router.cs | Adds subscription and unsubscription for the new NotFoundEvent. |
src/Components/Endpoints/src/DependencyInjection/HttpNavigationManager.cs | Updates NotFoundCore to set the 404 response via the endpoint HTML renderer. |
src/Components/Components/src/Routing/IHostEnvironmentNavigationManager.cs | Adds a new overload of Initialize() to support passing a renderer. |
src/Components/Endpoints/src/Rendering/EndpointHtmlRenderer.cs | Updates service initialization to pass the renderer instance. |
src/Components/Endpoints/src/RazorComponentEndpointInvoker.cs | Makes use of the new instance-based InitializeStandardComponentServicesAsync(). |
Test assets (.razor pages) | New pages added for testing NotFound behavior across different render modes. |
Copilot reviewed 20 out of 20 changed files in this pull request and generated no comments.
Comments suppressed due to low confidence (2)
src/Components/test/E2ETest/ServerRenderingTests/NoInteractivityTest.cs:40
- Ensure that concatenating _serverFixture.RootUri and relativeUrl does not inadvertently produce a double slash if the RootUri already ends with a '/'.
string absoluteUrl = "${_serverFixture.RootUri}/{relativeUrl}";
src/Components/Components/src/NavigationManager.cs:516
- [nitpick] The logic to handle single versus multiple not found handlers repeats similar patterns; consider refactoring this section into a shared helper method to improve maintainability.
if (handlerCount == 1) { ... }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Overview
This PR introduces a NotFound method on NavigationManager to support both SSR and interactive rendering, ensuring proper 404 handling and event notification. Key changes include:
- Implementation of NotFound behavior for both SSR (by setting the response status code to 404) and interactive rendering (by triggering the NotFoundEvent).
- Adjustments to public APIs and initialization methods to support optional renderer parameters.
- Addition of new tests to verify NotFound rendering in various scenarios.
Reviewed Changes
File | Description |
---|---|
src/Components/Components/src/NotFoundRenderingException.cs | Introduces a new exception for handling NotFound render failures. |
src/Components/Components/src/IEndpointHtmlRenderer.cs | Adds an interface method to set the 404 response. |
src/Components/test/E2ETest/ServerRenderingTests/RenderingTest.cs | Adds tests to verify interactive not found rendering. |
src/Components/Components/src/Routing/Router.cs | Subscribes to NotFoundEvent and implements a new event handler and logger method. |
src/Components/test/E2ETest/ServerRenderingTests/NoInteractivityTest.cs | Adds a test to verify SSR 404 behavior. |
src/Components/WebAssembly/WebAssembly/src/Services/WebAssemblyNavigationManager.cs | Implements NotFoundCore to trigger interactive not found notifications. |
src/Components/Endpoints/src/DependencyInjection/HttpNavigationManager.cs | Implements NotFoundCore by using the endpoint renderer to set a 404 response. |
src/Components/Server/src/Circuits/RemoteNavigationManager.cs | Provides a new Initialize overload and implements NotFoundCore with logging. |
src/Components/Components/src/Routing/IHostEnvironmentNavigationManager.cs | Extends the interface with an Initialize overload accepting a renderer. |
src/Components/Endpoints/test/RazorComponentResultTest.cs | Updates FakeNavigationManager to support the new overload. |
src/Components/Endpoints/src/Rendering/EndpointHtmlRenderer.EventDispatch.cs | Exposes a new method to set the 404 status code on the HTTP response. |
src/Components/Endpoints/src/Rendering/EndpointHtmlRenderer.cs | Updates initialization to pass the renderer instance. |
src/Components/Endpoints/src/RazorComponentEndpointInvoker.cs | Adjusts calls to use the instance method for component service initialization. |
src/Components/Components/src/NavigationManager.cs | Adds a new NotFound method, event, and related logging functionality. |
Razor components in test assets | Implements usage of NotFound in various rendering modes and updates routing test cases accordingly. |
Copilot reviewed 19 out of 19 changed files in this pull request and generated no comments.
Comments suppressed due to low confidence (1)
src/Components/Components/src/Routing/Router.cs:354
- Duplicate LoggerMessage id '1' is used in multiple logging methods. Consider assigning a unique id to each LoggerMessage to avoid potential conflicts.
[LoggerMessage(1, LogLevel.Debug, "Displaying {nameof(NotFound)} on request", EventName = "DisplayingNotFoundOnRequest")]
src/Components/Endpoints/src/DependencyInjection/HttpNavigationManager.cs
Outdated
Show resolved
Hide resolved
src/Components/Endpoints/src/Rendering/EndpointHtmlRenderer.EventDispatch.cs
Show resolved
Hide resolved
I re run but the failures look connected:
edit: |
NavigationManager
implementsNotFound
methodIn SSR, calling
NavigationManager.NotFound()
sets theHttpContext.Response.StatusCode
toStatus404NotFound
.In interactive, both WebAssembly and Server - it triggers the
NotifyNotFound
that risesNotFoundEvent
. Router, that is subscribed to that event, triggers the render ofNotFound
page if defined, orDefaultNotFoundContent
.Details
NotFound
, we cannot interrupt and we throwInvalidOperationException
.InitializeStandardComponentServicesAsync
from static to instance method.<NotFound>There's nothing here</NotFound>
and the SSR asserts the status code.System.InvalidOperationException : 'NavigationManagerProxy' has not been initialized.
because we trigger event subscription in http nav manager init method that expects the instance to be initialized.Fixes #45654