You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -777,36 +777,197 @@ When a component is rendered with a global interactive render mode, calling `Not
777
777
}
778
778
```
779
779
780
-
You can use the `OnNotFound` event for notifications when `NotFound` is invoked. The following example uses a render fragment (<xref:Microsoft.AspNetCore.Components.RenderFragment>) to render the Not Found content.
780
+
You can use the `OnNotFound` event for notifications when `NotFound` is invoked. The event is only fired when `NotFound` is called, not for any 404 response. For example, setting `HttpContextAccessor.HttpContext.Response.StatusCode` to `404` doesn't trigger `NotFound`/`OnNotFound`.
781
781
782
-
`Routes.razor`:
782
+
<!-- UPDATE 10.0 - For Pre5, the following can be expanded to
783
+
cover CSR with an added bit of coverage for
784
+
re-execution middleware. -->
785
+
786
+
In the following example for components that adopt [interactive server-side rendering (interactive SSR)](xref:blazor/fundamentals/index#client-and-server-rendering-concepts), custom content is rendered depending on where `OnNotFound` is called. If the event is triggered by the following `Movie` component when a movie isn't found on component initialization, a custom message states that the requested movie isn't found. If the event is triggered by the `User` component, a different message states that the user isn't found.
787
+
788
+
The following `NotFoundContext` service manages the context and the message for when content isn't found by components.
* The `NotFoundContext` service is injected, along with the <xref:Microsoft.AspNetCore.Components.NavigationManager>.
840
+
* In <xref:Microsoft.AspNetCore.Components.ComponentBase.OnInitializedAsync%2A>, `HandleNotFound` is an event handler assigned to the `OnNotFound` event. `HandleNotFound` calls `NotFoundContext.UpdateContext` to set a heading and message for Not Found content that's displayed by the `Router` component in the `Routes` component (`Routes.razor`).
841
+
* The components would normally use an ID from a route parameter to obtain a movie or user from a data store, such as a database. In the following examples, no entity is returned (`null`) to simulate what happens when an entity isn't found.
842
+
* When no entity is returned to <xref:Microsoft.AspNetCore.Components.ComponentBase.OnInitializedAsync%2A>, `NavigationManager.NotFound` is called, which in turn triggers the `OnNotFound` event and the `HandleNotFound` event handler. Not Found content is displayed by the router.
843
+
* The `HandleNotFound` method is unhooked on component disposal in <xref:System.IDisposable.Dispose%2A?displayProperty=nameWithType>.
844
+
845
+
`Movie` component (`Movie.razor`):
846
+
847
+
```razor
848
+
@page "/movie/{Id:int}"
849
+
@implements IDisposable
850
+
@inject NavigationManager NavigationManager
851
+
@inject NotFoundContext NotFoundContext
852
+
853
+
<div>
854
+
No matter what ID is used, no matching movie is returned
855
+
from the call to GetMovie().
856
+
</div>
794
857
795
858
@code {
796
-
private RenderFragment renderFragment =
797
-
@<div><h1>Not Found</h1><p>Sorry! Nothing to show.</p></div>;
To reach the preceding components in a local demonstration with a test app, create entries in the `NavMenu` component (`NavMenu.razor`) to reach the `Movie` and `User` components. The entity IDs, passed as route parameters, in the following example are mock values that have no effect because they aren't actually used by the components, which simulate not finding a movie or user.
954
+
955
+
In `NavMenu.razor`:
956
+
957
+
```razor
958
+
<div class="nav-item px-3">
959
+
<NavLink class="nav-link" href="movie/1">
960
+
<span class="bi bi-list-nested-nav-menu" aria-hidden="true"></span> Movie
961
+
</NavLink>
962
+
</div>
963
+
964
+
<div class="nav-item px-3">
965
+
<NavLink class="nav-link" href="user/2">
966
+
<span class="bi bi-list-nested-nav-menu" aria-hidden="true"></span> User
0 commit comments