QueryStringService Class
Provides services for synchronizing component properties with URL query string parameters.
public sealed class QueryStringServiceRemarks
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
| Name | Description |
|---|---|
| QueryStringService(NavigationManager, IJSRuntime) | Initializes a new instance of the QueryStringService class. |
Methods
| Name | Description |
|---|---|
| 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.csReads 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 : IComponentType parameters
T- The type of the component.
Parameters
componentT- The component whose properties should be populated from the query string.
UpdateQueryString<T>(T, bool, CancellationToken)
QueryStringService.csUpdates 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 : IComponentnet11.0
public ValueTask UpdateQueryString<T>(T component, bool reloadPage = true, CancellationToken cancellationToken = default) where T : IComponentType parameters
T- The type of the component.
Parameters
componentT- The component whose properties should be written to the query string.
reloadPagebool- 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. Whenfalse, the URL is updated in place using the browser's history API and nothing is re-rendered. cancellationTokenCancellationToken- A cancellation token to cancel the operation.
Returns
ValueTask A task that represents the asynchronous operation.