Access to browser navigation from Blazor is provided via the NavigationManager
service.
This can be injected into a Blazor component using @inject
in a razor file, or the [Inject]
attribute in a CS file.
The LocationChanged event
LocationChanged
is an event that is triggered whenever the URL in the browser is altered.
It passes an instance of LocationChangedEventArgs
which provides the following information:
The Location
property is the full URL as it appears in the browser, including the protocol, path, and any query string.
IsNavigationIntercepted indicates whether the navigation was initiated via code or via an HTML navigation.
false
The navigation was initiated byNavigationManager.NavigateTo
being called from code.true
The user clicked an HTML navigation element (such as ana href
) and Blazor intercepted the navigation instead of allowing the browser to actually navigate to a new URL, which would result in a request to the server. It will also be true in other cases, such as if someJavaScript
on the page causes a navigation (for example, after atimeOut
). Ultimately, any navigation event that wasn't initiated viaNavigationManager.NavigateTo
will be considered an intercepted navigation, and this value will betrue
.
Note there is currently no way to intercept a navigation and prevent it from proceeding.
Observing OnLocationChanged events
It is important to note that the NavigationManager
service is a long-living instance.
Consequently, any component that subscribes to its LocationChanged
event will be strongly referenced for the duration
of the service's lifetime.
It is therefore important our components also unsubscribe from this event when they are destroyed,
otherwise they will not be garbage collected.
Currently, the ComponentBase
class does not have a lifecycle
event for when it is destroyed, but it is possible to implement the IDisposable
interface.