我可以查找指定页面的路线列表吗?
我的代码类似于以下内容:
@page "/somepage/{Id:guid}/add"
@page "/somepage/{Id:guid}/remove"
@inject NavigationManager navigationManager
@implements IAsyncDisposable
if ( @remove ) {
<div>Remove!</div>
} else {
<div>Add!</div>
}
@code{
[Parameter]
public Guid Id { get; set; }
protected override void OnInitialized()
{
navigationManager.LocationChanged += HandleLocationChanged;
}
public async ValueTask DisposeAsync()
{
navigationManager.LocationChanged -= HandleLocationChanged;
}
protected override async Task OnParametersSetAsync()
{
remove = navigationManager.Uri.Contains("remove");
// Load some data...
await base.OnParametersSetAsync();
}
private void HandleLocationChanged(object? sender, LocationChangedEventArgs e)
{
var pageUris = new[] {
$"/somepage/{Id}/add",
$"/somepage/{Id}/remove"
};
var uri = navigationManager.Uri.Replace( navigationManager.BaseUri, "" );
if ( !pageUris.Contains(uri) ) return;
remove = navigationManager.Uri.Contains("remove");
// Load some data...
StateHasChanged();
}
}
在 HandleLocationChanged
事件中,我意识到如果用户单击指向完全不同页面的链接,则将为错误页面加载数据。因此,我添加了一个警卫来检查 NavigationManager.Uri
- 查看它是否指向不同的位置。
有什么方法可以让这个更通用吗?例如,我可以获取特定页面的路线列表,然后将其用于比较吗?
I have code similar to the following:
@page "/somepage/{Id:guid}/add"
@page "/somepage/{Id:guid}/remove"
@inject NavigationManager navigationManager
@implements IAsyncDisposable
if ( @remove ) {
<div>Remove!</div>
} else {
<div>Add!</div>
}
@code{
[Parameter]
public Guid Id { get; set; }
protected override void OnInitialized()
{
navigationManager.LocationChanged += HandleLocationChanged;
}
public async ValueTask DisposeAsync()
{
navigationManager.LocationChanged -= HandleLocationChanged;
}
protected override async Task OnParametersSetAsync()
{
remove = navigationManager.Uri.Contains("remove");
// Load some data...
await base.OnParametersSetAsync();
}
private void HandleLocationChanged(object? sender, LocationChangedEventArgs e)
{
var pageUris = new[] {
quot;/somepage/{Id}/add",
quot;/somepage/{Id}/remove"
};
var uri = navigationManager.Uri.Replace( navigationManager.BaseUri, "" );
if ( !pageUris.Contains(uri) ) return;
remove = navigationManager.Uri.Contains("remove");
// Load some data...
StateHasChanged();
}
}
In the HandleLocationChanged
event, I realised that if the user clicks on a link to a completely different page, then data will be loaded for the wrong page. So I added a guard to check NavigationManager.Uri
- to see if it points to a different location.
Is there some way to make this more generic? For example, can I get a list of routes for a particular page and then use that in my comparison?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题询问如何替换硬编码的
pageUris
变量:知道
@page "/foo"
会产生[RouteAttribute("foo")]
应用于页面生成的类时,可以直接创建一个函数来替换您的代码:这将为您拥有的每个
@page
指令生成一个元素,其中包含值:在您的问题中,您将其与
navigationManager.BaseUri
我认为这不是必需的,因为默认情况下 Blazor 应用程序会设置基本 url(即
),但如果您希望返回该值,则更改GetPageUrls
来合并它显然是微不足道的。最后,如果您想在其他地方使用它,则可以直接将其转换为扩展方法。
Your question asks how to replace your hardcoded
pageUris
variable:Knowing that
@page "/foo"
results in a[RouteAttribute("foo")]
being applied to your page's generated class, it's straightforward to create a function that will replace your code:This will produce one element for each
@page
directive you have, containing the values:In your question, you combine it with
navigationManager.BaseUri
which I believe should not be necessary, since by default a Blazor app sets the base url (i.e.<base href="/" />
), but if you want that returned, it's obviously trivial to changeGetPageUrls
to incorporate that as well.Finally, if you want to use this elsewhere, it should be straightforward to convert this into an extension method.