Net Core 3.5 Blazor可选int路由参数



试图弄清楚如何在不处理查询字符串的情况下使int路由参数成为可选参数。例如,如果我希望param1和param2是可选的。如果我在传入int参数时尝试将其留空,它将根本不匹配路由。所以

https://localhost/MyComponent///testString

不适用于下面的代码。我意识到我可以在0中通过,但这对我来说是一个好奇:(。

@page "/MyComponent/{param1:int}/{param2:int}/{param3}"
@code {
[Parameter] public int? Param1 { get; set; }
[Parameter] public int? Param2 { get; set; }
[Parameter] public string Param3 { get; set; }
...
}

您可以使用多个@page属性

MyComponent.razor

@page "/MyComponent"
@page "/MyComponent/{param3}"
@page "/MyComponent/{param1:int}/{param2:int}/{param3}"
@page "/MyComponent/{param1:int}/null/{param3}"
@page "/MyComponent/null/{param2:int}/{param3}"
@page "/MyComponent/null/null/{param3}"
<div class="d-flex flex-column">
<div>Param1 @Param1</div>
<div>Param2 @Param2</div>
<div>Param3 @Param3</div>
<a href="/MyComponent/Test 1">Test 1</a>
<a href="/MyComponent/1/2/Test 2">Test 2</a>
<a href="/MyComponent/3/null/Test 3">Test 3</a>
<a href="/MyComponent/null/4/Test 4">Test 4</a>
<a href="/MyComponent/null/null/Test 5">Test 5</a>
</div>
@code {
[Parameter] public int? Param1 { get; set; }
[Parameter] public int? Param2 { get; set; }
[Parameter] public string Param3 { get; set; }
}

对于enet。我从VS窗口复制了这个:

@page "/counter/{currentCount:int?}/{Param1:int?}"
<h1>Counter</h1>
<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
// private int currentCount = 0;
[Parameter] public int currentCount { get; set; } = 0;
[Parameter] public int Param1 { get; set; }

private void IncrementCount() {
currentCount++;
}
protected override void OnInitialized() {
currentCount = currentCount + Param1;

base.OnInitialized();
}
}

相关内容

  • 没有找到相关文章

最新更新