Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple query string parameters in Blazor routing

I have multiple working examples in my current project of retrieving 1 query parameter from the url however when trying to follow the same convention for multiple url params, I'm receiving the following error in the chrome debugger console:

Error: System.InvalidOperationException: 'Router' cannot find any component with a route for '/confirmemail'.

my page route is defined as:

@page "/confirmemail/{Token}/{UserId}"

and the @functions{...} section contains the following properties:

[Parameter]
string Token { get; set; }
[Parameter]
string UserId { get; set; }

I am trying to retrieve the query string parameters for a url that looks like this:

http://localhost:50466/confirmemail?Token=SomeReallyLargeToken&UserId=SomeGuidUserId

How can I achieve this?

like image 672
GregH Avatar asked Dec 14 '18 20:12

GregH


1 Answers

For anyone interested in how to pass and get the parameters in the query string style

http://localhost:50466/confirmemail?Token=SomeReallyLargeToken&UserId=SomeGuidUserId

the page route doesn't change

@page "/confirmemail/"

and you should get the parameters like

protected override void OnInitialized()
{
     var uri = navigationManager.ToAbsoluteUri(navigationManager.Uri); //you can use IURIHelper for older versions

     if(QueryHelpers.ParseQuery(uri.Query).TryGetValue("Token", out var token))
     {
         var token_par = token.First();
     }

     if(QueryHelpers.ParseQuery(uri.Query).TryGetValue("UserId", out var userid))
     {
         var userid_par = userid.First();
     }
}

Remember to add the following

@inject NavigationManager navigationManager
@using Microsoft.AspNetCore.WebUtilities @*for QueryHelpers*@

I'm using preview-9

like image 87
Antonio Correia Avatar answered Sep 29 '22 17:09

Antonio Correia