问题
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?
回答1:
Could you try?
http://localhost:50466/confirmemail/SomeReallyLargeToken/SomeGuidUserId
I think it should work.
UPDATE: If you want to get values exactly from query parameters good example is here https://learn-blazor.com/pages/router/
回答2:
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
回答3:
This ( /{Token}/{UserId}
) is not a pattern of query string. It is a part of the route template url(route parameters). This is a query string: ?Token=SomeReallyLargeToken&UserId=SomeGuidUserId
.
I'm not sure about it, right now, but this may work: http://localhost:50466/confirmemail/SomeReallyLargeToken/SomeGuidUserId
If you wish to access query parameters (not route parameters), you want to use the IUriHelper like this:
var uri = new Uri(UriHelper.GetAbsoluteUri());
Note that you should parse the returned value... I once saw a utility created by the Blazor community that does it.
Hope this helps...
来源:https://stackoverflow.com/questions/53786347/multiple-query-string-parameters-in-blazor-routing