skip to content

In Blazor, what does NavigationManager.NavigateTo do with forceLoad false versus true, and when must you force a full page load?

level: middleimportance: must knowfreq 55%

answer

  1. returns void, not a Task
  2. interactive router, enhanced navigation, or reload
  3. forceLoad: true means a new document
  4. non-Blazor endpoints need a real request
  5. NavigationException during static SSR

basics

~20 s

With forceLoad false, the default, NavigateTo stays inside Blazor: the interactive router swaps components or enhanced navigation patches the page. forceLoad true loads a new document, which non-Blazor endpoints and a fresh app state need.

solid answer

~50 s

`NavigateTo(uri, forceLoad: false)` keeps an internal URL inside Blazor. If the `Router` is interactive, it pushes a history entry, raises `LocationChanged` and renders the matching component without a new HTML document; otherwise `blazor.web.js` performs an enhanced navigation (a `fetch` whose HTML is patched into the DOM). `forceLoad: true` hands the URL to the browser as a full document load. Force it when the target is not a Blazor page (a file download from a Minimal API, an MVC or Identity endpoint), when you want a clean start after sign-in or sign-out, or to drop the current circuit or WebAssembly state. `replace: true` (or `NavigationOptions.ReplaceHistoryEntry`) replaces the history entry, and `Refresh(forceReload: true)` reloads the current page. During static SSR the call becomes an HTTP redirect; ASP.NET Core 10 throws `NavigationException` for that unless `BlazorDisableThrowNavigationException` is `true`, as the template sets it.

code

razor · 15 lines
razor
@inject NavigationManager Navigation

<button @onclick="OpenPlant">Details</button>
<button @onclick="DownloadCatalog">Download CSV</button>

@code {
    [Parameter] public int PlantId { get; set; }

    private void OpenPlant() =>
        Navigation.NavigateTo($"/plants/{PlantId}");

    // A Minimal API endpoint, not a component: needs a real request.
    private void DownloadCatalog() =>
        Navigation.NavigateTo("/exports/plants.csv", forceLoad: true);
}

go deeper

for a junior

Recall that NavigateTo navigates in code, that forceLoad defaults to false, and that forceLoad: true loads a new document from the server.

for a middle

Explain the three outcomes of a default NavigateTo (interactive router, enhanced navigation, reload), what LocationChanged reports, and how replace and Refresh behave.

for a senior

Show you can diagnose a swallowed NavigationException during static SSR, a not-found page instead of a download, and state that must be reset with a forced load.

for a principal

Discuss when a product should prefer full reloads at auth boundaries or between app areas, and the cost in circuits, downloads and lost client state.

## What `NavigateTo` is `NavigationManager` is the service a component injects (`@inject NavigationManager Navigation`) to read the current URL (`Uri`, `BaseUri`) and to navigate in code. `NavigateTo` returns `void`: it starts a navigation and returns at once, it does not wait for the next page to render. Its overloads are `NavigateTo(string uri, bool forceLoad = false, bool replace = false)` and `NavigateTo(string uri, NavigationOptions options)`, where `NavigationOptions` carries `ForceLoad`, `ReplaceHistoryEntry` and `HistoryEntryState`. ## `forceLoad: false`, the default What happens depends on where the component runs and on what the page around it can do: | Situation | What `NavigateTo("/plants/7")` does | |---|---| | The `Router` is interactive (a global render mode on `Routes`) | client-side routing: a history entry is pushed, `LocationChanged` fires, the router renders the matching component; no new HTML document is requested | | Per-page interactivity, `blazor.web.js` with enhanced navigation on | an **enhanced navigation**: `fetch` the page from the server and patch the returned HTML into the DOM | | Neither is available | a normal full page load | | The URL is outside the app's base URI | always a normal browser navigation | An interactive router has one trap. A link the user *clicks* to an unknown internal URL is retried as a full page load, because the path may belong to a non-Blazor endpoint. A *programmatic* `NavigateTo` to a URL that matches no component is not retried: the router renders its not-found content. So in a globally interactive nursery app, `NavigateTo("/exports/plants.csv")` aimed at a Minimal API endpoint shows the not-found page instead of downloading the file. ## `forceLoad: true` The browser loads the URL as a **new document**. Everything client-side starts over: the Blazor script boots again, an Interactive Server page gets a new circuit, and a WebAssembly app reloads its runtime. Use it when: 1. the target is not a Blazor page: a file from a Minimal API or controller, an MVC view, an Identity or OpenID Connect endpoint; 2. state must be rebuilt from scratch, for example after a sign-in or sign-out changed the authentication cookie; 3. you leave the app for an external site (any absolute URL outside the base URI is already treated this way). `NavigationManager.Refresh(bool forceReload = false)` is the same idea for the current page: without the flag it uses enhanced navigation when available, with `true` it always reloads the document. ## History and events - `replace: true` or `NavigationOptions.ReplaceHistoryEntry = true` replaces the current history entry, which suits a redirect after saving a form so that Back does not return to it. - `HistoryEntryState` stores a string with the entry; it is read back from `NavigationManager.HistoryEntryState`. - `LocationChanged` delivers `LocationChangedEventArgs` with `Location` and `IsNavigationIntercepted` (true for a link click, false for `NavigateTo`). Subscribe in `OnInitialized` and unsubscribe in `Dispose`; the event belongs to a scoped service that outlives the page. ## During static server-side rendering A statically rendered page has no browser connection, so the only way to navigate is an HTTP redirect. The framework does it this way: - By default `NavigateTo` throws a `NavigationException`. The endpoint renderer catches it and turns it into a redirect response, so code after the call never runs. - A `try`/`catch (Exception)` around the call swallows that exception, and the redirect silently disappears. - ASP.NET Core 10 adds the `BlazorDisableThrowNavigationException` MSBuild property. When it is `true`, the call no longer throws; the redirect is issued and the code after `NavigateTo` keeps running, as it does in interactive components. The SDK default is `false`; the .NET 10 Blazor Web App template writes `true` into the project file. The same rule applies to an interactive component during **prerendering**, which is also server rendering without a connection. ## Choosing in practice - Another Blazor page in the app: default `NavigateTo`. - A download, an MVC or Razor Pages page, or a sign-in or sign-out endpoint: `forceLoad: true`. - After saving, so Back does not return to the edit page: `replace: true`. - Re-reading server data for the current statically rendered page: `Refresh()`. ## Summary Default navigation stays inside Blazor and is cheap; a forced load is a fresh document and is the right tool whenever the destination or the required state lives outside the current Blazor session.

  • A static SSR page wraps NavigateTo in try/catch (Exception) and the redirect never happens. Why, and what are the fixes?
    By default `NavigateTo` during static SSR throws `NavigationException`, which the framework turns into the redirect. The broad `catch` swallows it. Either rethrow `NavigationException` (or catch narrower types), or on .NET 10 set `BlazorDisableThrowNavigationException` to `true` so the call no longer throws and the redirect is issued normally.
  • What is the difference between NavigationManager.Refresh() and Refresh(true)?
    `Refresh()` reloads the current page through enhanced navigation when it is available, so the new server HTML is patched in and client-side state survives where possible; otherwise it reloads. `Refresh(forceReload: true)` always performs a full document reload.
  • In a globally interactive app, why does NavigateTo("/exports/plants.csv") show the not-found page?
    With an interactive `Router`, a programmatic navigation to an internal URL is routed client-side. The router finds no component for that path and, because the navigation was not an intercepted link click, renders its not-found content rather than retrying as a full load. `forceLoad: true` sends a real request to the endpoint.

  • NavigateTo returns a Task you await until the next page has rendered
  • forceLoad: false always sends a new HTTP request for the full HTML document
  • In an interactive app, NavigateTo to a Minimal API URL always reaches the endpoint
  • During static SSR, catching Exception around NavigateTo is harmless in every .NET version
  • replace: true reloads the page from the server instead of adding a history entry

Assumes ASP.NET Core 10: during static SSR NavigateTo throws NavigationException unless the BlazorDisableThrowNavigationException MSBuild property is true (the .NET 10 Blazor Web App template sets it; the SDK default is false).