20

Click the a TAB to pass multiple parameters. How to receive

<a href="../navigatetopage?id="1"&key="img"></a>

In the page you want to navigate to, add the parameter to your route:

@page "/navigatetopage/"

[Parameter]
private string myvalue{ get; set; }
0

4 Answers 4

33

The easiest way is to use Route parameters instead of QueryString:

@page "/navigatetopage/{id:int}/{key}"

@code 
{
    [Parameter]
    public int Id { get; set; }

    [Parameter]
    public string Key { get; set; }
}

And the url looks like:

<a href="../navigatetopage/1/img"></a>

Or if you do want to query string, set the property / field within OnParametersSet():

@page "/navigatetopage/"

@code 
{
    public int Id { get; set; }

    public string Key { get; set; }

    protected override void OnParametersSet()
    {
        var qs = navManager.ToAbsoluteUri(navManager.Uri).Query;
        var query = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(qs);

        if (query.TryGetValue("id", out var idStr) && int.TryParse(idStr, out var id))
        {
            Id = id;
        }

        if (query.TryGetValue("Key", out var key))
        {
            Key = key;
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Could this be achieved in the same way with 2 separate GUIDs?
@itminus With the query string approach, OnParametersSet() is not invoked when we navigate within the same page (i.e. only update the query string), correct?
8

A very simple solution to pass several parameters to a page is to use [SupplyParameterFromQuery(Name = "myParam")]

@code {
    [Parameter]
    [SupplyParameterFromQuery(Name = "id")]
    public int myId { get; set; }

    [Parameter]
    [SupplyParameterFromQuery(Name = "Key")]
    public String myKey { get; set; }
}

and it is then useless to change the routing @page "navigatetopage".

We can also write /navigatetopage?id=1&key=img

Comments

4

Using Blazor Server I wanted to be able to pass multiple route parameters using NavigateTo. It took me way longer than it should have to get an answer that worked for me so I thought I would share:

On the component I want to navigate to:

@page "/nextpage/caseId/{caseId:int}/showSecrets/{showSecrets:bool}

On the component, I want to navigate from after injecting NavigationManager nav:

int caseID = 17;  //for example <br>
nav.NavigateTo("nextpage/caseId/" + caseId +"/showSecrets/" + true);

Hope this helps someone.

Comments

0

I know that this question is 3 years old but I feel this might help.

What you are looking for is passing multiple parameters using (Query strings) Microsoft has a great explanation of it here

https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/routing?view=aspnetcore-7.0#query-strings

I believe this will solve the issue:

@page "/navigatetopage/"

[Parameter]
[SupplyParameterFromQuery]
private string id{ get; set; }

[Parameter]
[SupplyParameterFromQuery]
private string key{ get; set; }

Note: I believe you use [SupplyParameterFromQuery(Name = "myParam")] only when your parameters are named differently than the Query parameters, or you have something like an array and you want to fill it from the Query

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.