问题
I'm current setup:
- .NET core 3 (preview 6)
- Blazor server side rendering
In a Blazor page I have something like the following:
@page "/page"
@page "/page/{Id}"
With:
[Parameter]
public string Id { get; set; }
Now, when navigating from, for example, /page/1
to /page/2
, I can use OnParametersSetAsync
to detect these changes. But the problem occurs when navigating from /page/1
to /page
. OnParametersSetAsync
can detect this change, however the Id
parameter will stay as 1
from the previous route.
I was wondering what i could do in this situation.
回答1:
Blazor doesn't override empty parameters with null, do it by yourself:
@page "/Counter"
@page "/Counter/{Id}"
<h1>Current Id: @IdString;</h1>
<a href="/Counter"> go home </a> |
<a href="/Counter/1"> go home 1 </a> |
<a href="/Counter/2"> go home 2 </a>
@code
{
private string IdString;
[Parameter] public string Id { get; set; }
protected override async Task OnParametersSetAsync()
{
// copy par value somewhere
IdString = Id?.ToString() ?? "";
// rest parm (set to null) by yourself
Id=null;
}
}
来源:https://stackoverflow.com/questions/56869508/blazor-route-changes-in-same-page