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 NavigationManager
service has two members that are of particular interest; NavigateTo
and LocationChanged
.
The LocationChanged
event will be explained in more detail in Detecting navigation events.
The NavigateTo method
The NavigationManager``.NavigateTo
method enables C# code to control the browser's URL.
As with intercepted navigations, the browser does not actually navigate to a new URL.
Instead the URL in the browser is replaced, and the previous URL is inserted into the browser's navigation history,
but no request is made to the server for the content of the new page.
Navigations made via NavigateTo
will cause the LocationChanged
event to trigger,
passing the new URL and false
for IsNavigationIntercepted
.
For this example we'll alter the standard Blazor template again. We'll use what we learned previously in Route parameters and Optional route parameters.
First, delete the Index.razor
and FetchData.razor
pages,
and remove the links to those pages inside the NavMenu.razor
file.
Also in NavMenu, change the href
of the link to counter to href=""
, because we are going to make it the default page.
Edit Counter.razor
and give it two routes, "/"
and "/counter/{CurrentCount:int}"
We'll also need to change the currentCount
field so it is a property with a getter and setter, and decorate it as a [Parameter]
.
Note that it has been renamed from camelCase to PascalCase too.
We now have a counter page that can be accessed either simply be reaching the main page of the app,
or by specifying /counter/X
, where X is an integer value.
The NavigationManager
was injected into our CounterBase
class, and so is accessible in our Counter.razor
file.
We'll call the AlterBy
method from two buttons, one to increment the CurrentCount
and one to decrement it.
There is also an option the user will be able to select, forceLoad
,
which will set the relevant parameter in the call to NavigateTo
so we can see the difference.
The whole file should eventually look like this:
Clicking the -
or +
buttons will call the AlterBy
method which will instruct the NavigationManager
service to
navigate to /counter/X
, where X is the value of the adjusted CurrentCount
- resulting in the following output in
the browser's console:
Clicking the Reset link will result in an Intercepted navigation (i.e. not initiated in C# code) and
navigate to /counter/0
, resetting the value of CurrentCount
.
ForceLoad
The forceLoad
parameter instructs Blazor to bypass its own routing system and instead have the browser actually
navigate to the new URL.
This will result in an HTTP request to the server to retrieve the content to display.
Note that a force load is not required in order to navigate to an off-site URL.
Calling NavigateTo
to another domain will invoke a full browser navigation.
Play with the GitHub example for this section.
Look in the browser's Console window to see how IsNavigationIntercepted
differs when navigating via the buttons and
the Reset link, and look in the browser's Network window to see how it behaves differently based on whether you are:
- Navigating with
forceLoad
set tofalse
. - Navigating with
forceLoad
set totrue
. - Navigating to an off-site URL.
To observe the last scenario, you may wish to update your AdjustBy
method to navigate off-site when CurrentValue
passes a specific value.