In this article, we will know about .NET6 feature like 'SupplyParameterFromQuery' attribute to access the query parameters in the blazor application.
Sample URL:
SupplyParameterFromQuery Attribute:
- A routable component to capture the query parameter values into the component parameter, then we have to decorate the 'SupplyParameterFromQuery' attribute along with the 'Parameter' attribute on top of the component parameter.
- On decorating 'SupplyParameterFromQuery', then the component parameters can read the query parameters of type 'bool', 'datetime', 'decimal', 'float', 'guid', 'int', 'long', 'string'. It also read the array type of query parameters as well.
- If component parameter name and query parameter name then there is no need for explicit mapping to read the values. If the component parameter name and query parameter name are different then we have to specify the query parameter in an attribute like '[SupplyParameterFromQuery(Name="query-parameter-name")]'
https://localhost:7271/test?isredcar=true&carcost=20000.45&carname=honda&feature=ac&feature=4%20door&feature=auto%20drive
- Query parameter 'isredcar' value boolean type
- Query parameter 'carcost' value decimal type
- Query parameter 'carname' value string type
- Query parameter 'feature' value string array type. To capture the array of values, the same query parameter will added multiple times in the URL.
Create A .NET6 Blazor WebAssembly Application:
Let's create a .NET6 Blazor WebAssembly application to accomplish our demo. The most recommended IDE's are 'Visual Studio 2022', 'Visual Studio Code'(CLI Commands). Here I'm using 'Visual Studio Code'(CLI Commands) editor.
CLI command For Minimal API Project
dotnet new blazorwasm -o Your_Project_Name
dotnet new blazorwasm -o Your_Project_Name
Read Query Parameters Using SupplyParameterFromQuery Attribute:
Here we will read all query params and bind them to our UI. So let's create Blazor Component like 'Sample.razor'.
Pages/Sample.razor:(HTML Part)
@page "/test" <div> <span>Car Name</span> - <span>@CarName</span> <br /> <span>IS Red Color Car</span> - <span>@IsRedCar.ToString()</span> <br /> <span>Car Cost</span> - <span>@CarCost</span> <br /> <span>Features:</span> <ul> @foreach (var feature in Features) { <li>@feature</li> } </ul> </div>Pages/Sample.razor:(C# Part)
@code { [Parameter] [SupplyParameterFromQuery(Name = "isredcar")] public bool IsRedCar { get; set; } [Parameter] [SupplyParameterFromQuery] public decimal CarCost { get; set; } [Parameter] [SupplyParameterFromQuery] public string? CarName { get; set; } [Parameter] [SupplyParameterFromQuery(Name = "feature")] public string[] Features { get; set; } }
- Here each query parameter will be assigned to the respective component parameter that is possible due to decorating 'Parameter', 'SupplyParameterFromQuery' attributes.
- Here the 'Features' is an array type variable. So to read the array of values from the URL, query parameter should be added multiple times
Support Me!
Buy Me A Coffee
PayPal Me
Video Session:
Wrapping Up:
Hopefully, I think this article delivered some useful information on the SupplyParameterFromQuery attribute in the Blazor Application. I love to have your feedback, suggestions, and better techniques in the comment section below.
Well Explained
ReplyDelete