From a9c615e8327e44952c7a8fd0bd0729ad518a9bbe Mon Sep 17 00:00:00 2001 From: Robert Haken Date: Tue, 20 Feb 2024 13:03:38 +0100 Subject: [PATCH] #31845 Routing with optional parameters and fallback value - OnInitialized vs. OnParametersSet (#31859) --- aspnetcore/blazor/fundamentals/routing.md | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/aspnetcore/blazor/fundamentals/routing.md b/aspnetcore/blazor/fundamentals/routing.md index 2971d2ccce..e6b926642b 100644 --- a/aspnetcore/blazor/fundamentals/routing.md +++ b/aspnetcore/blazor/fundamentals/routing.md @@ -304,14 +304,7 @@ Optional parameters aren't supported. In the following example, two [`@page` dir :::moniker-end -Use [`OnParametersSet`](xref:blazor/components/lifecycle#after-parameters-are-set-onparameterssetasync) instead of [`OnInitialized{Async}`](xref:blazor/components/lifecycle#component-initialization-oninitializedasync) to permit app navigation to the same component with a different optional parameter value. Based on the preceding example, use `OnParametersSet` when the user should be able to navigate from `/route-parameter-2` to `/route-parameter-2/amazing` or from `/route-parameter-2/amazing` to `/route-parameter-2`: - -```csharp -protected override void OnParametersSet() -{ - Text = Text ?? "fantastic"; -} -``` +When the [`OnInitialized{Async}`](xref:blazor/components/lifecycle#component-initialization-oninitializedasync) method is used instead of [`OnParametersSet`](xref:blazor/components/lifecycle#after-parameters-are-set-onparameterssetasync), the default assignment of the `Text` property to `fantastic` doesn't occur if the user navigates within the same component. For example, this situation arises when the user navigates from `/route-parameter-2/amazing` to `/route-parameter-2`. As the component instance persists and accepts new parameters, the `OnInitialized` method isn't invoked again. :::moniker range="< aspnetcore-6.0"