diff --git a/aspnetcore/blazor/fundamentals/routing.md b/aspnetcore/blazor/fundamentals/routing.md index e7a433d3940a..6e681314967b 100644 --- a/aspnetcore/blazor/fundamentals/routing.md +++ b/aspnetcore/blazor/fundamentals/routing.md @@ -515,6 +515,50 @@ Use to manage URIs and :::moniker-end +## Location changes + +For the event, provides the following information about navigation events: + +* : The URL of the new location. +* : If `true`, Blazor intercepted the navigation from the browser. If `false`, caused the navigation to occur. + +The following component: + +* Navigates to the app's `Counter` component (`Counter.razor`) when the button is selected using . +* Handles the location changed event by subscribing to . + * 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: + + > :::no-loc text="BlazorSample.Pages.Navigate: Information: URL of new location: https://localhost:{PORT}/counter"::: + +`Navigate.razor`: + +:::moniker range=">= aspnetcore-7.0" + +:::code language="razor" source="~/../blazor-samples/7.0/BlazorSample_WebAssembly/Pages/routing/Navigate.razor"::: + +:::moniker-end + +:::moniker range=">= aspnetcore-6.0 < aspnetcore-7.0" + +:::code language="razor" source="~/../blazor-samples/6.0/BlazorSample_WebAssembly/Pages/routing/Navigate.razor"::: + +:::moniker-end + +:::moniker range=">= aspnetcore-5.0 < aspnetcore-6.0" + +:::code language="razor" source="~/../blazor-samples/5.0/BlazorSample_WebAssembly/Pages/routing/Navigate.razor"::: + +:::moniker-end + +:::moniker range="< aspnetcore-5.0" + +:::code language="razor" source="~/../blazor-samples/3.1/BlazorSample_WebAssembly/Pages/routing/Navigate.razor"::: + +:::moniker-end + +For more information on component disposal, see . + :::moniker range=">= aspnetcore-8.0" ## Enhanced navigation and form handling @@ -532,8 +576,12 @@ Blazor Web Apps are capable of two types of routing for page navigation and form * The feature isn't [explicitly disabled](xref:blazor/fundamentals/startup#disable-enhanced-navigation-and-form-handling). * The destination URL is within the internal base URI space (the app's base path). +If server-side routing and enhanced navigation are enabled, [location changing handlers](#location-changes) are only invoked for programmatic navigations initiated from an interactive runtime. In future releases, additional types of navigations, such as link clicks, may also invoke location changing handlers. + If enhanced navigation is available for interactive client routing, navigation always goes through the interactive client-side router. This point is only relevant in an uncommon scenario where an interactive `` is nested inside a server-side rendered ``. In that case, the interactive router takes priority when handling navigation, so enhanced navigation isn't used for navigation because it's a server-side routing feature. +When an enhanced navigation occurs, [`LocationChanged` event handlers](#location-changes) registered with interactive server and WebAssembly runtimes are typically invoked. There are cases when location changing handlers might not intercept an enhanced navigation. For example, the user might switch to another page before an interactive runtime becomes available. Therefore, it's important that app logic not rely on invoking a location changing handler, as there's no guarantee of the handler executing. + When calling : * If `forceLoad` is `false`, which is the default: @@ -657,50 +705,6 @@ Passing `https://localhost:8001/segment` in `inputURI` results in the following > :::no-loc text="System.ArgumentException: 'The URI 'https://localhost:8001/segment' is not contained by the base URI 'https://localhost:8000/'.'"::: -## Location changes - -For the event, provides the following information about navigation events: - -* : The URL of the new location. -* : If `true`, Blazor intercepted the navigation from the browser. If `false`, caused the navigation to occur. - -The following component: - -* Navigates to the app's `Counter` component (`Counter.razor`) when the button is selected using . -* Handles the location changed event by subscribing to . - * 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: - - > :::no-loc text="BlazorSample.Pages.Navigate: Information: URL of new location: https://localhost:{PORT}/counter"::: - -`Navigate.razor`: - -:::moniker range=">= aspnetcore-7.0" - -:::code language="razor" source="~/../blazor-samples/7.0/BlazorSample_WebAssembly/Pages/routing/Navigate.razor"::: - -:::moniker-end - -:::moniker range=">= aspnetcore-6.0 < aspnetcore-7.0" - -:::code language="razor" source="~/../blazor-samples/6.0/BlazorSample_WebAssembly/Pages/routing/Navigate.razor"::: - -:::moniker-end - -:::moniker range=">= aspnetcore-5.0 < aspnetcore-6.0" - -:::code language="razor" source="~/../blazor-samples/5.0/BlazorSample_WebAssembly/Pages/routing/Navigate.razor"::: - -:::moniker-end - -:::moniker range="< aspnetcore-5.0" - -:::code language="razor" source="~/../blazor-samples/3.1/BlazorSample_WebAssembly/Pages/routing/Navigate.razor"::: - -:::moniker-end - -For more information on component disposal, see . - :::moniker range=">= aspnetcore-7.0" ## Navigation history state