I think what you are looking for is an optional Route parameter.
Since .Net5, optional route parameters exist natively. Just add a ?
at the end of the parameter.
@page "/Claimaction/{ClaimsId:int}"
@* if navigation without id, should be possible, this route is needed too *@
@page "/Claimaction"
@* for .NET 5 only on @page directive is needed
@page "/Claimaction/{ClaimsId:int?}"
*@
@code{
[Parameter]
public Int32? ClaimsId { get; set; }
public override async Task OnParametersSetAsync()
{
await base.OnParametersSetAsync();
ClaimsId = ClaimsId ?? -1; // set default value if not set via routing or input
}
}
Because the ClaimsId
has the name as the router parameter {ClaimsId:int}
, it is bound when used via Url like /ClaimsAction/10235 (Way 1)
Because ClaimsId
is a Parameter
you can set it via binding or any other way. (Way 2). Even if a @page
directive exists, you could still use it as a "normal" component nested inside other components or your layout.
Because ClaimsId
is a nullable type, it doesn't require a value at all. If no value is received either via routing or input, the value will be set to -1
. To achieve this behavior, we override SetParamterAsync
and set a default value there ( ClaimsId = ClaimsId ?? -1;
).
I would recommend reading this tutorial too.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…