QueryStringService Class

Provides services for synchronizing component properties with URL query string parameters.

public sealed class QueryStringService

Remarks

This service allows you to bind component properties to query string parameters, making it easy to create shareable URLs that preserve component state. Properties must be marked with SupplyParameterFromQueryAttribute to be synchronized. To use this service, register it in your dependency injection container using QueryStringServiceExtensions.AddQueryStringParameters.

Example

Register the service in Program.cs: builder.Services.AddQueryStringParameters(); Use it in a component:

@inject QueryStringService QueryStringService

<input type="search" value="@SearchTerm" @onchange="e => UpdateSearch(e.Value as string)" />

@code {
    [Parameter]
    [SupplyParameterFromQuery]
    public string? SearchTerm { get; set; }

    [Parameter]
    [SupplyParameterFromQuery]
    public int PageNumber { get; set; } = 1;

    protected override void OnInitialized()
    {
        // Read query string parameters and populate properties
        QueryStringService.SetParametersFromQueryString(this);
    }

    private async Task UpdateSearch(string? newSearchTerm)
    {
        SearchTerm = newSearchTerm;
        // Update the URL with new query string parameters
        await QueryStringService.UpdateQueryString(this);
    }
}

Constructors

NameDescription
QueryStringService(NavigationManager, IJSRuntime)Initializes a new instance of the QueryStringService class.

QueryStringService(NavigationManager, IJSRuntime)

QueryStringService.cs

Initializes a new instance of the QueryStringService class.

public QueryStringService(NavigationManager navigationManager, IJSRuntime jsRuntime)

Parameters

navigationManager NavigationManager
The navigation manager to use for URL operations.
jsRuntime IJSRuntime
The JavaScript runtime to use for history manipulation.

Methods

NameDescription
SetParametersFromQueryString<T>(T)Reads query string parameters from the current URL and sets them on the component properties marked with SupplyParameterFromQueryAttribute.
UpdateQueryString<T>(T, bool, CancellationToken)Updates the URL query string with values from component properties marked with SupplyParameterFromQueryAttribute.

SetParametersFromQueryString<T>(T)

QueryStringService.cs

Reads query string parameters from the current URL and sets them on the component properties marked with SupplyParameterFromQueryAttribute.

public void SetParametersFromQueryString<T>(T component) where T : IComponent

Type parameters

T
The type of the component.

Parameters

component T
The component whose properties should be populated from the query string.

UpdateQueryString<T>(T, bool, CancellationToken)

QueryStringService.cs

Updates the URL query string with values from component properties marked with SupplyParameterFromQueryAttribute.

net10.0

public ValueTask UpdateQueryString<T>(T component, bool reloadPage = true, CancellationToken cancellationToken = default) where T : IComponent

net11.0

public ValueTask UpdateQueryString<T>(T component, bool reloadPage = true, CancellationToken cancellationToken = default) where T : IComponent

Type parameters

T
The type of the component.

Parameters

component T
The component whose properties should be written to the query string.
reloadPage bool
When true (the default), the new URL is applied by navigating to it, so the Blazor router re-renders the page. This is a client-side navigation, not a full browser reload. When false, the URL is updated in place using the browser's history API and nothing is re-rendered.
cancellationToken CancellationToken
A cancellation token to cancel the operation.

Returns

ValueTask A task that represents the asynchronous operation.