Preserve Query Strings During Programmatic Navigation in Blazor
Use NavigationManager and UriBuilder to keep query strings intact when navigating between Blazor components in Server and WebAssembly apps.
08 Dec 2025, 04:47 UTC

Problem: Programmatic Navigation Drops Query Strings
A Blazor component calls NavigationManager.NavigateTo to open a details page, but the address bar loses the current filter, sort, or page parameters. Developers often try to fix this by concatenating strings, which introduces encoding errors and behaves differently between Blazor Server and Blazor WebAssembly.
Takeaway: build the destination URI with System.UriBuilder starting from NavigationManager.Uri, replace only the path, keep the existing query and fragment, then pass the result to NavigateTo. This preserves query strings in both hosting models without manual string assembly.
Prerequisites
- A Blazor Server or Blazor WebAssembly project targeting .NET 6 or later.
NavigationManagerinjected into the component that initiates navigation.- Browser history access, which is enabled by default in both hosting models.
Preserve Query Strings with UriBuilder
NavigationManager is the built-in Blazor service for reading the current URI and triggering navigation. UriBuilder is a .NET type that assembles a URI from components such as scheme, host, path, query, and fragment.
This example keeps the current query string while changing only the path to an item details page:
@page "/items"
@inject NavigationManager NavManager
@code {
private void OpenItem(string itemId)
{
var current = new Uri(NavManager.Uri);
var builder = new UriBuilder(current)
{
Path = $"/items/{itemId}",
Query = current.Query.TrimStart('?')
};
NavManager.NavigateTo(builder.Uri.ToString());
}
}
current.Query includes the leading question mark. UriBuilder.Query expects the value without it, so the example trims the leading ? before assigning. Fragments are handled the same way through builder.Fragment.
By default, NavigateTo performs client-side navigation. Pass true as the second argument only when you intentionally want a full browser reload:
NavManager.NavigateTo(builder.Uri.ToString(), forceLoad: true);
Server and WebAssembly Handle Navigation Differently
The destination URI is built the same way in both models, but the runtime behavior differs:
| Behavior | Blazor Server | Blazor WebAssembly |
|---|---|---|
| Navigation mechanism | Updates the URI over the SignalR circuit without reloading the host HTML page. | Calls the browser history API (pushState) without a full page fetch. |
| Base path sensitivity | Less sensitive; the server processes the route. | Requires a correct <base href="/"> in wwwroot/index.html so relative URIs resolve properly. |
| Component state | Preserved while the circuit is alive; the default idle timeout is five minutes. | Retained unless the page fully reloads. |
| Absolute external URL | Triggers full browser navigation. | Triggers full browser navigation. |
Use TryNavigateTo for Navigation Guards
TryNavigateTo returns false when navigation is blocked. Use it to guard routes, for example when a form has unsaved changes:
private bool AttemptNavigate(string nextPath)
{
if (HasUnsavedChanges)
{
return false;
}
return NavManager.TryNavigateTo(nextPath);
}
When the method returns false, the application should show its own confirmation dialog; Blazor does not display one automatically.
Verify the Result
- Run the project from a terminal in the project folder:
dotnet watch run. No special permissions are required. Expected result: Kestrel hosts the app and a browser opens. Risk: port conflicts if another process uses the configured port. - Open a page that includes a query string, for example
/items?category=tools&page=2. - Trigger the programmatic navigation.
- Check the address bar: the path should change while
category=tools&page=2remains. - Open browser developer tools, select the Network tab, and confirm no full document request occurs for client-side navigation.
- In Blazor WebAssembly, check the History panel for a
pushStateentry. In Blazor Server, confirm the SignalR connection stays open.
Limitations and Recovery
- Manual query-string assembly can break encoding.
UriBuilderreduces this risk, but avoid appending raw user input directly to the query string. - Blazor Server circuits expire after the idle timeout. After expiry, reconnection may require re-authentication and can reset component state.
- Blazor WebAssembly with an incorrect base href resolves relative navigation targets incorrectly.
- Absolute URLs in
NavigateTobypass the Blazor router and reload the page.
There is no persistent state to roll back. The browser Back button returns to the previous URI and query string. In Blazor Server, if the circuit has expired, the user signs in again and the application reloads.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.