A query string stores values in the URL that are visible to users. It is mostly used to pass the data from one page to another. In Blazor, the query string can be added using the NavigationManager. You can pass multiple parameters through the URL and access it via queryhelpers,
1. Install the package Microsoft.AspNetCore.WebUtilities from NuGet.
2. Use the QueryHelpers.ParseQuery.TryGetValue method
@page "/queryparam"
@inject NavigationManager navManager
<h3>Query Paramter Demo</h3>
<div>Id = @Id</div>
<div>Name = @Name</div>
@code{
string Id { get; set; }
string Name { get; set; }
protected override void OnInitialized()
{
GetId();
GetName();
}
public void GetId()
{
var uri = new Uri(navManager.Uri);
Id = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(uri.Query).TryGetValue("id", out var type) ? type.First() : "";
}
public void GetName()
{
var uri = new Uri(navManager.Uri);
Name = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(uri.Query).TryGetValue("name", out var type) ? type.First() : "";
}
}
Share with