From 6fe6c8beff75318e04bdb727853d3fc3f87ebe22 Mon Sep 17 00:00:00 2001
From: guardrex <1622880+guardrex@users.noreply.github.com>
Date: Fri, 2 Sep 2022 07:27:26 -0500
Subject: [PATCH 01/17] Routing features (loc changes, NavigationLock)
---
aspnetcore/blazor/fundamentals/routing.md | 114 ++++++++++++++++++++++
1 file changed, 114 insertions(+)
diff --git a/aspnetcore/blazor/fundamentals/routing.md b/aspnetcore/blazor/fundamentals/routing.md
index b4d7dd0547f2..b3392f78cca4 100644
--- a/aspnetcore/blazor/fundamentals/routing.md
+++ b/aspnetcore/blazor/fundamentals/routing.md
@@ -1543,8 +1543,122 @@ Use to manage URIs and
| | An event that fires when the navigation location has changed. For more information, see the [Location changes](#location-changes) section. |
| | Converts a relative URI into an absolute URI. |
| | Given a base URI (for example, a URI previously returned by ), converts an absolute URI into a URI relative to the base URI prefix. |
+| `RegisterLocationChangingHandler` | Registers a handler to process incoming navigation events. |
| | Returns a URI constructed by updating with a single parameter added, updated, or removed. For more information, see the [Query strings](#query-strings) section. |
+In the following example, a location changing handler is registered for navigation events. Note that calling always invokes any location changing handlers that exist.
+
+`Pages/NavHandler.razor`:
+
+```razor
+@page "/nav-handler"
+@inject NavigationManager NavigationManager
+
+
+
+
+
+
+@code {
+ protected override void OnInitialized()
+ {
+ NavigationManager.RegisterLocationChangingHandler(OnLocationChanging);
+ }
+
+ private async ValueTask OnLocationChanging(LocationChangingContext context)
+ {
+ if (context.TargetLocation == "counter")
+ {
+ context.PreventNavigation();
+ }
+ }
+}
+```
+
+`LocationChangingContext` properties:
+
+* `TargetLocation`: Obtain the target location.
+* `HistoryEntryState`: The state associated with the target history entry.
+* `IsNavigationIntercepted`: Whether the navigation was intercepted from a link.
+* `CancellationToken`: Gets a to determine if the navigation was canceled, for example, to determine if the user triggered a different navigation.
+* `PreventNavigation`: Called to prevent the navigation from continuing.
+
+Pass `NavigationOptions` to to control the following behaviors:
+
+* `ForceLoad`: Bypass client-side routing and force the browser to load the new page from the server, whether or not the URI is handled by the client-side router. The default value is `false`.
+* `ReplaceHistoryEntry`: Replace the current entry in the history stack. If `false`, append the new entry to the history stack. The default value is `false`.
+* `HistoryEntryState`: Gets or sets the state to append to the history entry.
+
+```csharp
+NavigationManager.NavigateTo("/path", new NavigationOptions
+{
+ HistoryEntryState = "Navigation state",
+});
+```
+
+Since internal navigation can be canceled asynchronously, multiple overlapping navigations may occur. For example, the user is rapidly clicking the back button on a page or clicking multiple links before a navigation is determined. The following is a summary of the asynchronous navigation logic:
+
+* If any location changing handlers are registered, all navigations are initially reverted, then replayed if the navigation isn't canceled.
+* If overlapping navigation requests are made, the latest request always cancels earlier requests, which means the following:
+ * The app may treat multiple back and forward button clicks as a single click.
+ * If the user clicks multiple links before the navigation completes, the last link clicked determines the navigation.
+
+For more information, see the following components in the `BasicTestApp` (`dotnet/aspnetcore` reference source):
+
+* [`NavigationManagerComponent`](https://github.com/dotnet/aspnetcore/blob/main/src/Components/test/testassets/BasicTestApp/RouterTest/NavigationManagerComponent.razor)
+* [`ConfigurableNavigationLock`](https://github.com/dotnet/aspnetcore/blob/main/src/Components/test/testassets/BasicTestApp/RouterTest/ConfigurableNavigationLock.razor)
+
+[!INCLUDE[](~/includes/aspnetcore-repo-ref-source-links.md)]
+
+## Intercept location changes in a component
+
+The `NavigationLock` component is used to intercept navigation events:
+
+* The component only takes effect after it's rendered, just like any other Razor component.
+* It's possible for the suer to bypass interception of the navigation because the user navigated away before the component had a chance to render.
+
+In the following `NavigationLockExample` component:
+
+* `OnBeforeInternalNavigation` sets a callback to be invoked when an internal navigation event occurs. `PreventNavigation` is called to prevent naviation from occurring if the user declines to confirm the navigation via a [JavaScript (JS) interop call](xref:blazor/js-interop/call-javascript-from-dotnet) that spawns the [JS `confirm` dialog](https://developer.mozilla.org/docs/Web/API/Window/confirm).
+* `ConfirmExternalNavigation` sets a browser dialog to prompt the user to either confirm or cancel the external navigation. The default value is `false`.
+
+`Pages/NavLock.razor`:
+
+```razor
+@page "/nav-lock"
+@inject IJSRuntime JSRuntime
+@inject NavigationManager NavigationManager
+
+
+
+
+
+
+
+@code {
+ private void Navigate()
+ {
+ NavigationManager.NavigateTo("/");
+ }
+
+ private async Task OnBeforeInternalNavigation(LocationChangingContext context)
+ {
+ var isConfirmed = await JSRuntime.InvokeAsync("confirm",
+ "Are you sure you want to navigate to the Index page?");
+
+ if (!isConfirmed)
+ {
+ context.PreventNavigation();
+ }
+ }
+}
+```
+
## Location changes
For the event, provides the following information about navigation events:
From 6d3a721a82587036e2297bda13bf0b7dd293bf5c Mon Sep 17 00:00:00 2001
From: guardrex <1622880+guardrex@users.noreply.github.com>
Date: Tue, 6 Sep 2022 07:07:42 -0500
Subject: [PATCH 02/17] Updates
---
aspnetcore/blazor/fundamentals/routing.md | 236 +++++++++++-----------
1 file changed, 122 insertions(+), 114 deletions(-)
diff --git a/aspnetcore/blazor/fundamentals/routing.md b/aspnetcore/blazor/fundamentals/routing.md
index b3392f78cca4..3f89c66c4a3a 100644
--- a/aspnetcore/blazor/fundamentals/routing.md
+++ b/aspnetcore/blazor/fundamentals/routing.md
@@ -1543,122 +1543,9 @@ Use to manage URIs and
| | An event that fires when the navigation location has changed. For more information, see the [Location changes](#location-changes) section. |
| | Converts a relative URI into an absolute URI. |
| | Given a base URI (for example, a URI previously returned by ), converts an absolute URI into a URI relative to the base URI prefix. |
-| `RegisterLocationChangingHandler` | Registers a handler to process incoming navigation events. |
+| [`RegisterLocationChangingHandler`](#handle-location-changes-in-a-component) | Registers a handler to process incoming navigation events. Calling always invokes the handler. |
| | Returns a URI constructed by updating with a single parameter added, updated, or removed. For more information, see the [Query strings](#query-strings) section. |
-In the following example, a location changing handler is registered for navigation events. Note that calling always invokes any location changing handlers that exist.
-
-`Pages/NavHandler.razor`:
-
-```razor
-@page "/nav-handler"
-@inject NavigationManager NavigationManager
-
-
-
-
-
-
-@code {
- protected override void OnInitialized()
- {
- NavigationManager.RegisterLocationChangingHandler(OnLocationChanging);
- }
-
- private async ValueTask OnLocationChanging(LocationChangingContext context)
- {
- if (context.TargetLocation == "counter")
- {
- context.PreventNavigation();
- }
- }
-}
-```
-
-`LocationChangingContext` properties:
-
-* `TargetLocation`: Obtain the target location.
-* `HistoryEntryState`: The state associated with the target history entry.
-* `IsNavigationIntercepted`: Whether the navigation was intercepted from a link.
-* `CancellationToken`: Gets a to determine if the navigation was canceled, for example, to determine if the user triggered a different navigation.
-* `PreventNavigation`: Called to prevent the navigation from continuing.
-
-Pass `NavigationOptions` to to control the following behaviors:
-
-* `ForceLoad`: Bypass client-side routing and force the browser to load the new page from the server, whether or not the URI is handled by the client-side router. The default value is `false`.
-* `ReplaceHistoryEntry`: Replace the current entry in the history stack. If `false`, append the new entry to the history stack. The default value is `false`.
-* `HistoryEntryState`: Gets or sets the state to append to the history entry.
-
-```csharp
-NavigationManager.NavigateTo("/path", new NavigationOptions
-{
- HistoryEntryState = "Navigation state",
-});
-```
-
-Since internal navigation can be canceled asynchronously, multiple overlapping navigations may occur. For example, the user is rapidly clicking the back button on a page or clicking multiple links before a navigation is determined. The following is a summary of the asynchronous navigation logic:
-
-* If any location changing handlers are registered, all navigations are initially reverted, then replayed if the navigation isn't canceled.
-* If overlapping navigation requests are made, the latest request always cancels earlier requests, which means the following:
- * The app may treat multiple back and forward button clicks as a single click.
- * If the user clicks multiple links before the navigation completes, the last link clicked determines the navigation.
-
-For more information, see the following components in the `BasicTestApp` (`dotnet/aspnetcore` reference source):
-
-* [`NavigationManagerComponent`](https://github.com/dotnet/aspnetcore/blob/main/src/Components/test/testassets/BasicTestApp/RouterTest/NavigationManagerComponent.razor)
-* [`ConfigurableNavigationLock`](https://github.com/dotnet/aspnetcore/blob/main/src/Components/test/testassets/BasicTestApp/RouterTest/ConfigurableNavigationLock.razor)
-
-[!INCLUDE[](~/includes/aspnetcore-repo-ref-source-links.md)]
-
-## Intercept location changes in a component
-
-The `NavigationLock` component is used to intercept navigation events:
-
-* The component only takes effect after it's rendered, just like any other Razor component.
-* It's possible for the suer to bypass interception of the navigation because the user navigated away before the component had a chance to render.
-
-In the following `NavigationLockExample` component:
-
-* `OnBeforeInternalNavigation` sets a callback to be invoked when an internal navigation event occurs. `PreventNavigation` is called to prevent naviation from occurring if the user declines to confirm the navigation via a [JavaScript (JS) interop call](xref:blazor/js-interop/call-javascript-from-dotnet) that spawns the [JS `confirm` dialog](https://developer.mozilla.org/docs/Web/API/Window/confirm).
-* `ConfirmExternalNavigation` sets a browser dialog to prompt the user to either confirm or cancel the external navigation. The default value is `false`.
-
-`Pages/NavLock.razor`:
-
-```razor
-@page "/nav-lock"
-@inject IJSRuntime JSRuntime
-@inject NavigationManager NavigationManager
-
-
-
-
-
-
-
-@code {
- private void Navigate()
- {
- NavigationManager.NavigateTo("/");
- }
-
- private async Task OnBeforeInternalNavigation(LocationChangingContext context)
- {
- var isConfirmed = await JSRuntime.InvokeAsync("confirm",
- "Are you sure you want to navigate to the Index page?");
-
- if (!isConfirmed)
- {
- context.PreventNavigation();
- }
- }
-}
-```
-
## Location changes
For the event, provides the following information about navigation events:
@@ -1681,6 +1568,23 @@ The following component:
For more information on component disposal, see .
+## Navigation options
+
+Pass `NavigationOptions` to to control the following behaviors:
+
+* `ForceLoad`: Bypass client-side routing and force the browser to load the new page from the server, whether or not the URI is handled by the client-side router. The default value is `false`.
+* `ReplaceHistoryEntry`: Replace the current entry in the history stack. If `false`, append the new entry to the history stack. The default value is `false`.
+* `HistoryEntryState`: Gets or sets the state to append to the history entry.
+
+```csharp
+NavigationManager.NavigateTo("/path", new NavigationOptions
+{
+ HistoryEntryState = "Navigation state"
+});
+```
+
+For more information on the history stack, see the [Handle location changes in a component](#handle-location-changes-in-a-component) section.
+
## Query strings
Use the [`[SupplyParameterFromQuery]` attribute](xref:Microsoft.AspNetCore.Components.SupplyParameterFromQueryAttribute) with the [`[Parameter]` attribute](xref:Microsoft.AspNetCore.Components.ParameterAttribute) to specify that a component parameter of a routable component can come from the query string.
@@ -2016,6 +1920,110 @@ In the following `App` component example:
> [!NOTE]
> Not throwing if the cancellation token in is canceled can result in unintended behavior, such as rendering a component from a previous navigation.
+## Handle location changes in a component
+
+`RegisterLocationChangingHandler` registers a handler to process incoming navigation events. The handler's context (`LocationChangingContext`) provides the following properties:
+
+* `TargetLocation`: Gets the target location.
+* `HistoryEntryState`: Gets the state associated with the target history entry.
+* `IsNavigationIntercepted`: Gets whether the navigation was intercepted from a link.
+* `CancellationToken`: Gets a to determine if the navigation was canceled, for example, to determine if the user triggered a different navigation.
+* `PreventNavigation`: Called to prevent the navigation from continuing.
+
+A component can register multiple location changing handlers, and calling always invokes all of the registered handlers.
+
+In the following example, a location changing handler is registered for navigation events.
+
+`Pages/NavHandler.razor`:
+
+```razor
+@page "/nav-handler"
+@inject NavigationManager NavigationManager
+
+
+
+
+
+
+@code {
+ protected override void OnInitialized()
+ {
+ NavigationManager.RegisterLocationChangingHandler(OnLocationChanging);
+ }
+
+ private async ValueTask OnLocationChanging(LocationChangingContext context)
+ {
+ if (context.TargetLocation == "counter")
+ {
+ context.PreventNavigation();
+ }
+ }
+}
+```
+
+Since internal navigation can be canceled asynchronously, multiple overlapping navigations may occur. For example, multiple handler calls may occur when the user rapidly selects the back button on a page or selects multiple links before a navigation is determined. The following is a summary of the asynchronous navigation logic:
+
+* If any location changing handlers are registered, all navigations are initially reverted, then replayed if the navigation isn't canceled.
+* If overlapping navigation requests are made, the latest request always cancels earlier requests, which means the following:
+ * The app may treat multiple back and forward button selections as a single selection.
+ * If the user selects multiple links before the navigation completes, the last link selected determines the navigation.
+
+For more information on passing `NavigationOptions` to to control entries and state of the navigation history stack, see the [Navigation options](#navigation-options) section.
+
+For additional example code, see the [`NavigationManagerComponent` in the `BasicTestApp` (`dotnet/aspnetcore` reference source)](https://github.com/dotnet/aspnetcore/blob/main/src/Components/test/testassets/BasicTestApp/RouterTest/NavigationManagerComponent.razor).
+
+[!INCLUDE[](~/includes/aspnetcore-repo-ref-source-links.md)]
+
+To facilitate the control of navigation events in a Razor component, use the `NavigationLock` component. After the `NavigationLock` component is rendered, it intercepts navigation events.
+
+In the following `NavigationLockExample` component:
+
+* `ConfirmExternalNavigation` sets a browser dialog to prompt the user to either confirm or cancel the external navigation. The default value is `false`. An attempt to follow the link to Microsoft's website must be confirmed by the user before the navigation to `https://www.microsoft.com` succeeds.
+* `OnBeforeInternalNavigation` sets a callback to be invoked when an internal navigation event occurs. `PreventNavigation` is called to prevent naviation from occurring if the user declines to confirm the navigation via a [JavaScript (JS) interop call](xref:blazor/js-interop/call-javascript-from-dotnet) that spawns the [JS `confirm` dialog](https://developer.mozilla.org/docs/Web/API/Window/confirm).
+
+`Pages/NavLock.razor`:
+
+```razor
+@page "/nav-lock"
+@inject IJSRuntime JSRuntime
+@inject NavigationManager NavigationManager
+
+
+
+
@code {
- private
+ private IDisposable registration;
protected override void OnInitialized()
{
From 8578fe9445ad0aa353ab7212444a47eb8f995f81 Mon Sep 17 00:00:00 2001
From: guardrex <1622880+guardrex@users.noreply.github.com>
Date: Tue, 6 Sep 2022 13:09:00 -0500
Subject: [PATCH 08/17] Updates
---
aspnetcore/blazor/fundamentals/routing.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/aspnetcore/blazor/fundamentals/routing.md b/aspnetcore/blazor/fundamentals/routing.md
index ae73ff98b724..a7d28aaf494c 100644
--- a/aspnetcore/blazor/fundamentals/routing.md
+++ b/aspnetcore/blazor/fundamentals/routing.md
@@ -1951,7 +1951,7 @@ In the following example, a location changing handler is registered for navigati
@code {
- private IDisposable registration;
+ private IDisposable? registration;
protected override void OnInitialized()
{
From 0cbb3f591c46dde7e59d300bd8f9dc2807266448 Mon Sep 17 00:00:00 2001
From: guardrex <1622880+guardrex@users.noreply.github.com>
Date: Tue, 6 Sep 2022 13:11:44 -0500
Subject: [PATCH 09/17] Updates
---
aspnetcore/blazor/fundamentals/routing.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/aspnetcore/blazor/fundamentals/routing.md b/aspnetcore/blazor/fundamentals/routing.md
index a7d28aaf494c..bd2477b674f6 100644
--- a/aspnetcore/blazor/fundamentals/routing.md
+++ b/aspnetcore/blazor/fundamentals/routing.md
@@ -1955,7 +1955,7 @@ In the following example, a location changing handler is registered for navigati
protected override void OnInitialized()
{
- var registration =
+ registration =
NavigationManager.RegisterLocationChangingHandler(OnLocationChanging);
}
From c2468fb4e626fdf48d402c0de129818be4f72581 Mon Sep 17 00:00:00 2001
From: Luke Latham <1622880+guardrex@users.noreply.github.com>
Date: Tue, 6 Sep 2022 13:42:58 -0500
Subject: [PATCH 10/17] Apply suggestions from code review
Co-authored-by: Mackinnon Buck
---
aspnetcore/blazor/fundamentals/routing.md | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/aspnetcore/blazor/fundamentals/routing.md b/aspnetcore/blazor/fundamentals/routing.md
index bd2477b674f6..55012888ff9a 100644
--- a/aspnetcore/blazor/fundamentals/routing.md
+++ b/aspnetcore/blazor/fundamentals/routing.md
@@ -1930,7 +1930,7 @@ In the following `App` component example:
* `CancellationToken`: Gets a to determine if the navigation was canceled, for example, to determine if the user triggered a different navigation.
* `PreventNavigation`: Called to prevent the navigation from continuing.
-A component can register multiple location changing handlers, and calling invokes all of the registered handlers. [Dispose registered handlers](xref:blazor/components/lifecycle#component-disposal-with-idisposable-and-iasyncdisposable) to avoid memory leaks.
+A component can register multiple location changing handlers, and calling invokes all of the registered handlers. [Dispose registered handlers](xref:blazor/components/lifecycle#component-disposal-with-idisposable-and-iasyncdisposable) to unregister them.
In the following example, a location changing handler is registered for navigation events. Note that the component implements `IDisposable` and disposes the handler in its `Dispose` method.
@@ -1984,7 +1984,7 @@ For additional example code, see the [`NavigationManagerComponent` in the `Basic
[!INCLUDE[](~/includes/aspnetcore-repo-ref-source-links.md)]
-To facilitate the control of navigation events in a Razor component, use the `NavigationLock` component. After the `NavigationLock` component is rendered, it intercepts navigation events. Handlers associated with `NavigationLock` components are disposed automatically when the component is disposed.
+To facilitate the control of navigation events in a Razor component, use the `NavigationLock` component. As long as the `NavigationLock` component is rendered, it intercepts navigation events. Handlers associated with `NavigationLock` components are disposed automatically when the component is disposed.
In the following `NavLock` component:
From 9d9ea04b5758cb9c4cd065ef0f0e4156ab88c23b Mon Sep 17 00:00:00 2001
From: guardrex <1622880+guardrex@users.noreply.github.com>
Date: Tue, 6 Sep 2022 13:49:33 -0500
Subject: [PATCH 11/17] Updates
---
aspnetcore/blazor/fundamentals/routing.md | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/aspnetcore/blazor/fundamentals/routing.md b/aspnetcore/blazor/fundamentals/routing.md
index 55012888ff9a..04e20c3447f6 100644
--- a/aspnetcore/blazor/fundamentals/routing.md
+++ b/aspnetcore/blazor/fundamentals/routing.md
@@ -1930,7 +1930,11 @@ In the following `App` component example:
* `CancellationToken`: Gets a to determine if the navigation was canceled, for example, to determine if the user triggered a different navigation.
* `PreventNavigation`: Called to prevent the navigation from continuing.
-A component can register multiple location changing handlers, and calling invokes all of the registered handlers. [Dispose registered handlers](xref:blazor/components/lifecycle#component-disposal-with-idisposable-and-iasyncdisposable) to unregister them.
+A component can register multiple location changing handlers. Navigations invoke all of the location changing handlers registered across the entire app (across multiple components), and any internal navigation executes them all in parallel. In addition to invoking the handlers, selecting internal links (those that point to URLs under the app's base path) and navigating using the forward/back buttons also invoke the handlers.
+
+Handlers are only executed for internal navigations within the app. If the user selects a link that navigates to a different site or changes the address bar to a different site manually, location changing handlers aren't executed.
+
+[Dispose registered handlers](xref:blazor/components/lifecycle#component-disposal-with-idisposable-and-iasyncdisposable) to unregister them.
In the following example, a location changing handler is registered for navigation events. Note that the component implements `IDisposable` and disposes the handler in its `Dispose` method.
From 2b469842494ad8adb1248ef58710579618074e10 Mon Sep 17 00:00:00 2001
From: guardrex <1622880+guardrex@users.noreply.github.com>
Date: Tue, 6 Sep 2022 14:02:45 -0500
Subject: [PATCH 12/17] Updates
---
aspnetcore/blazor/fundamentals/routing.md | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/aspnetcore/blazor/fundamentals/routing.md b/aspnetcore/blazor/fundamentals/routing.md
index 04e20c3447f6..91e47b2b9545 100644
--- a/aspnetcore/blazor/fundamentals/routing.md
+++ b/aspnetcore/blazor/fundamentals/routing.md
@@ -1583,7 +1583,7 @@ NavigationManager.NavigateTo("/path", new NavigationOptions
});
```
-For more information on obtaining the state associated with the target history entry while handling location changes, see the [Handle location changes in a component](#handle-location-changes-in-a-component) section.
+For more information on obtaining the state associated with the target history entry while handling location changes, see the [Handle (prevent) location changes](#handle-prevent-location-changes) section.
## Query strings
@@ -1920,7 +1920,7 @@ In the following `App` component example:
> [!NOTE]
> Not throwing if the cancellation token in is canceled can result in unintended behavior, such as rendering a component from a previous navigation.
-## Handle location changes in a component
+## Handle (prevent) location changes
`RegisterLocationChangingHandler` registers a handler to process incoming navigation events. The handler's context provided by `LocationChangingContext` includes the following properties:
@@ -1930,7 +1930,10 @@ In the following `App` component example:
* `CancellationToken`: Gets a to determine if the navigation was canceled, for example, to determine if the user triggered a different navigation.
* `PreventNavigation`: Called to prevent the navigation from continuing.
-A component can register multiple location changing handlers. Navigations invoke all of the location changing handlers registered across the entire app (across multiple components), and any internal navigation executes them all in parallel. In addition to invoking the handlers, selecting internal links (those that point to URLs under the app's base path) and navigating using the forward/back buttons also invoke the handlers.
+A component can register multiple location changing handlers. Navigations invoke all of the location changing handlers registered across the entire app (across multiple components), and any internal navigation executes them all in parallel. In addition to handlers are invoked:
+
+* When selecting internal links, which are links that point to URLs under the app's base path.
+* When navigating using the forward and back buttons in a browser.
Handlers are only executed for internal navigations within the app. If the user selects a link that navigates to a different site or changes the address bar to a different site manually, location changing handlers aren't executed.
From 1f602b6a83e374269ca70f4f750b57bb347029a5 Mon Sep 17 00:00:00 2001
From: guardrex <1622880+guardrex@users.noreply.github.com>
Date: Tue, 6 Sep 2022 14:13:39 -0500
Subject: [PATCH 13/17] Updates
---
aspnetcore/blazor/fundamentals/routing.md | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/aspnetcore/blazor/fundamentals/routing.md b/aspnetcore/blazor/fundamentals/routing.md
index 91e47b2b9545..de434f76abf9 100644
--- a/aspnetcore/blazor/fundamentals/routing.md
+++ b/aspnetcore/blazor/fundamentals/routing.md
@@ -1583,7 +1583,7 @@ NavigationManager.NavigateTo("/path", new NavigationOptions
});
```
-For more information on obtaining the state associated with the target history entry while handling location changes, see the [Handle (prevent) location changes](#handle-prevent-location-changes) section.
+For more information on obtaining the state associated with the target history entry while handling location changes, see the [Handle/prevent location changes](#handleprevent-location-changes) section.
## Query strings
@@ -1920,7 +1920,7 @@ In the following `App` component example:
> [!NOTE]
> Not throwing if the cancellation token in is canceled can result in unintended behavior, such as rendering a component from a previous navigation.
-## Handle (prevent) location changes
+## Handle/prevent location changes
`RegisterLocationChangingHandler` registers a handler to process incoming navigation events. The handler's context provided by `LocationChangingContext` includes the following properties:
From b69d3703eb949accc8143b25ab0adda95bbe60c8 Mon Sep 17 00:00:00 2001
From: Luke Latham <1622880+guardrex@users.noreply.github.com>
Date: Wed, 7 Sep 2022 05:24:08 -0500
Subject: [PATCH 14/17] Update aspnetcore/blazor/fundamentals/routing.md
Co-authored-by: Mackinnon Buck
---
aspnetcore/blazor/fundamentals/routing.md | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/aspnetcore/blazor/fundamentals/routing.md b/aspnetcore/blazor/fundamentals/routing.md
index de434f76abf9..5fa46030dd82 100644
--- a/aspnetcore/blazor/fundamentals/routing.md
+++ b/aspnetcore/blazor/fundamentals/routing.md
@@ -1960,10 +1960,13 @@ In the following example, a location changing handler is registered for navigati
@code {
private IDisposable? registration;
- protected override void OnInitialized()
+ protected override void OnAfterRender(bool firstRender)
{
- registration =
- NavigationManager.RegisterLocationChangingHandler(OnLocationChanging);
+ if (firstRender)
+ {
+ registration =
+ NavigationManager.RegisterLocationChangingHandler(OnLocationChanging);
+ }
}
private async ValueTask OnLocationChanging(LocationChangingContext context)
From df1148e62f069dbff7a72a02590bb2eeb723170f Mon Sep 17 00:00:00 2001
From: guardrex <1622880+guardrex@users.noreply.github.com>
Date: Wed, 7 Sep 2022 06:22:55 -0500
Subject: [PATCH 15/17] Updates
---
aspnetcore/blazor/fundamentals/routing.md | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/aspnetcore/blazor/fundamentals/routing.md b/aspnetcore/blazor/fundamentals/routing.md
index 5fa46030dd82..f6576d038606 100644
--- a/aspnetcore/blazor/fundamentals/routing.md
+++ b/aspnetcore/blazor/fundamentals/routing.md
@@ -1930,14 +1930,14 @@ In the following `App` component example:
* `CancellationToken`: Gets a to determine if the navigation was canceled, for example, to determine if the user triggered a different navigation.
* `PreventNavigation`: Called to prevent the navigation from continuing.
-A component can register multiple location changing handlers. Navigations invoke all of the location changing handlers registered across the entire app (across multiple components), and any internal navigation executes them all in parallel. In addition to handlers are invoked:
+A component can register multiple location changing handlers in its [`OnAfterRender` or `OnAfterRenderAsync` methods](xref:blazor/components/lifecycle#after-component-render-onafterrenderasync). Navigations invoke all of the location changing handlers registered across the entire app (across multiple components), and any internal navigation executes them all in parallel. In addition to handlers are invoked:
* When selecting internal links, which are links that point to URLs under the app's base path.
* When navigating using the forward and back buttons in a browser.
Handlers are only executed for internal navigations within the app. If the user selects a link that navigates to a different site or changes the address bar to a different site manually, location changing handlers aren't executed.
-[Dispose registered handlers](xref:blazor/components/lifecycle#component-disposal-with-idisposable-and-iasyncdisposable) to unregister them.
+Implement and dispose registered handlers to unregister them. For more information, see .
In the following example, a location changing handler is registered for navigation events. Note that the component implements `IDisposable` and disposes the handler in its `Dispose` method.
@@ -1994,12 +1994,17 @@ For additional example code, see the [`NavigationManagerComponent` in the `Basic
[!INCLUDE[](~/includes/aspnetcore-repo-ref-source-links.md)]
-To facilitate the control of navigation events in a Razor component, use the `NavigationLock` component. As long as the `NavigationLock` component is rendered, it intercepts navigation events. Handlers associated with `NavigationLock` components are disposed automatically when the component is disposed.
+To facilitate the control of navigation events in a Razor component, use the `NavigationLock` component. As long as the `NavigationLock` component is rendered, it intercepts navigation events.
+
+`NavigationLock` parameters:
+
+* `ConfirmExternalNavigation` sets a browser dialog to prompt the user to either confirm or cancel external navigation. The default value is `false`. Displaying the confirmation dialog requires initial user interaction with the page before triggering external navigation with the URL in the browser's address bar. For more information on the interaction requirement, see [Window: `beforeunload` event (MDN documentation)](https://developer.mozilla.org/docs/Web/API/Window/beforeunload_event).
+* `OnBeforeInternalNavigation` sets a callback for internal navigation events.
In the following `NavLock` component:
-* `ConfirmExternalNavigation` sets a browser dialog to prompt the user to either confirm or cancel the external navigation. The default value is `false`. An attempt to follow the link to Microsoft's website must be confirmed by the user before the navigation to `https://www.microsoft.com` succeeds.
-* `OnBeforeInternalNavigation` sets a callback to be invoked when an internal navigation event occurs. `PreventNavigation` is called to prevent naviation from occurring if the user declines to confirm the navigation via a [JavaScript (JS) interop call](xref:blazor/js-interop/call-javascript-from-dotnet) that spawns the [JS `confirm` dialog](https://developer.mozilla.org/docs/Web/API/Window/confirm).
+* An attempt to follow the link to Microsoft's website must be confirmed by the user before the navigation to `https://www.microsoft.com` succeeds.
+* `PreventNavigation` is called to prevent naviation from occurring if the user declines to confirm the navigation via a [JavaScript (JS) interop call](xref:blazor/js-interop/call-javascript-from-dotnet) that spawns the [JS `confirm` dialog](https://developer.mozilla.org/docs/Web/API/Window/confirm).
`Pages/NavLock.razor`:
From 4a7a428923091cf8d185fff7198b9e435f91ad57 Mon Sep 17 00:00:00 2001
From: guardrex <1622880+guardrex@users.noreply.github.com>
Date: Wed, 7 Sep 2022 06:40:23 -0500
Subject: [PATCH 16/17] Updates
---
aspnetcore/blazor/fundamentals/routing.md | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/aspnetcore/blazor/fundamentals/routing.md b/aspnetcore/blazor/fundamentals/routing.md
index f6576d038606..8112a205bedc 100644
--- a/aspnetcore/blazor/fundamentals/routing.md
+++ b/aspnetcore/blazor/fundamentals/routing.md
@@ -226,7 +226,7 @@ The following component:
* The `HandleLocationChanged` method is unhooked when `Dispose` is called by the framework. Unhooking the method permits garbage collection of the component.
* The logger implementation logs the following information when the button is selected:
- > `BlazorSample.Pages.Navigate: Information: URL of new location: https://localhost:5001/counter`
+ > :::no-loc text="BlazorSample.Pages.Navigate: Information: URL of new location: https://localhost:{PORT}/counter":::
`Pages/Navigate.razor`:
@@ -863,7 +863,7 @@ The following component:
* The `HandleLocationChanged` method is unhooked when `Dispose` is called by the framework. Unhooking the method permits garbage collection of the component.
* The logger implementation logs the following information when the button is selected:
- > `BlazorSample.Pages.Navigate: Information: URL of new location: https://localhost:5001/counter`
+ > :::no-loc text="BlazorSample.Pages.Navigate: Information: URL of new location: https://localhost:5001/counter":::
`Pages/Navigate.razor`:
@@ -1247,7 +1247,7 @@ The following component:
* The `HandleLocationChanged` method is unhooked when `Dispose` is called by the framework. Unhooking the method permits garbage collection of the component.
* The logger implementation logs the following information when the button is selected:
- > `BlazorSample.Pages.Navigate: Information: URL of new location: https://localhost:5001/counter`
+ > :::no-loc text="BlazorSample.Pages.Navigate: Information: URL of new location: https://localhost:5001/counter":::
`Pages/Navigate.razor`:
@@ -1560,7 +1560,7 @@ The following component:
* The `HandleLocationChanged` method is unhooked when `Dispose` is called by the framework. Unhooking the method permits garbage collection of the component.
* The logger implementation logs the following information when the button is selected:
- > `BlazorSample.Pages.Navigate: Information: URL of new location: https://localhost:{PORT}/counter`
+ > :::no-loc text="BlazorSample.Pages.Navigate: Information: URL of new location: https://localhost:{PORT}/counter":::
`Pages/Navigate.razor`:
@@ -1939,7 +1939,7 @@ Handlers are only executed for internal navigations within the app. If the user
Implement and dispose registered handlers to unregister them. For more information, see .
-In the following example, a location changing handler is registered for navigation events. Note that the component implements `IDisposable` and disposes the handler in its `Dispose` method.
+In the following example, a location changing handler is registered for navigation events.
`Pages/NavHandler.razor`:
@@ -2004,7 +2004,7 @@ To facilitate the control of navigation events in a Razor component, use the `Na
In the following `NavLock` component:
* An attempt to follow the link to Microsoft's website must be confirmed by the user before the navigation to `https://www.microsoft.com` succeeds.
-* `PreventNavigation` is called to prevent naviation from occurring if the user declines to confirm the navigation via a [JavaScript (JS) interop call](xref:blazor/js-interop/call-javascript-from-dotnet) that spawns the [JS `confirm` dialog](https://developer.mozilla.org/docs/Web/API/Window/confirm).
+* `PreventNavigation` is called to prevent navigation from occurring if the user declines to confirm the navigation via a [JavaScript (JS) interop call](xref:blazor/js-interop/call-javascript-from-dotnet) that spawns the [JS `confirm` dialog](https://developer.mozilla.org/docs/Web/API/Window/confirm).
`Pages/NavLock.razor`:
From a2e7bc86a73395b2c3d6a8fc6af788fabc10bb3d Mon Sep 17 00:00:00 2001
From: guardrex <1622880+guardrex@users.noreply.github.com>
Date: Wed, 7 Sep 2022 06:51:06 -0500
Subject: [PATCH 17/17] Updates
---
aspnetcore/blazor/fundamentals/routing.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/aspnetcore/blazor/fundamentals/routing.md b/aspnetcore/blazor/fundamentals/routing.md
index 8112a205bedc..5ed784995f62 100644
--- a/aspnetcore/blazor/fundamentals/routing.md
+++ b/aspnetcore/blazor/fundamentals/routing.md
@@ -1543,7 +1543,7 @@ Use to manage URIs and
| | An event that fires when the navigation location has changed. For more information, see the [Location changes](#location-changes) section. |
| | Converts a relative URI into an absolute URI. |
| | Given a base URI (for example, a URI previously returned by ), converts an absolute URI into a URI relative to the base URI prefix. |
-| [`RegisterLocationChangingHandler`](#handle-location-changes-in-a-component) | Registers a handler to process incoming navigation events. Calling always invokes the handler. |
+| [`RegisterLocationChangingHandler`](#handleprevent-location-changes) | Registers a handler to process incoming navigation events. Calling always invokes the handler. |
| | Returns a URI constructed by updating with a single parameter added, updated, or removed. For more information, see the [Query strings](#query-strings) section. |
## Location changes