razorblazor-server-side.net-7.0blazor-routing

Passing Query string to the Razor component using [SupplyParameterFromQuery] attribute is not working


Passing Query String to the Razor component using SupplyParameterFromQuery attribute is not working.

QueryStringTest.razor

@page "/querytest"

<h3>Query String Test</h3>
<p>Query string supplied: <code>@FirstName</code></p>

@code 
{
    [SupplyParameterFromQuery(Name = "user")]
    public string? FirstName { get; set; }
}

Index.razor

@page "/"
@inject NavigationManager navigation

<PageTitle>Index</PageTitle>

<h1>Hello, world!</h1>

Welcome to your new app.

<SurveyPrompt Title="How is Blazor working for you?" />

<button @onclick="NavigateToQueryPage" class="btn btn-primary">Click to Navigate</button>

@code
{
    private void NavigateToQueryPage()
    {
        navigation.NavigateTo("/querytest?user=somedata");
    }
}

Even tried in NavMenu.razor but doesn't work

<div class="nav-item px-3">
    <NavLink class="nav-link" href="querytest?user=mydata">
        <span class="oi oi-list-rich" aria-hidden="true"></span> Query String
    </NavLink>
</div>

Query string property is always null but the navigation alone works.

[SupplyParameterFromQuery(Name = "user")]
public string? FirstName { get; set; }

enter image description here

Does this feature still in development or what? Blazor QueryString


Solution

  • If you're running this is Net7 then you need to declare the parameter like this:

    [Parameter][SupplyParameterFromQuery(Name = "user")] public string? FirstName { get; set; }
    

    In Net8 you can declare it as you did without the [Parameter].