我可以查找指定页面的路线列表吗?

发布于 2025-01-20 08:54:01 字数 1486 浏览 2 评论 0原文

我的代码类似于以下内容:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼睛会笑 2025-01-27 08:54:01

您的问题询问如何替换硬编码的 pageUris 变量:

var pageUris = new[] {
    $"{navigationManager.BaseUri}somepage/add",
    $"{navigationManager.BaseUri}somepage/remove"
};

知道 @page "/foo" 会产生 [RouteAttribute("foo")] 应用于页面生成的类时,可以直接创建一个函数来替换您的代码:

private string[] GetPageUrls()
{
    return GetType().GetCustomAttributes<RouteAttribute>().Select(x => x.Template).ToArray();
}

这将为您拥有的每个 @page 指令生成一个元素,其中包含值:

"/somepage/add", "/somepage/remove"

在您的问题中,您将其与navigationManager.BaseUri 我认为这不是必需的,因为默认情况下 Blazor 应用程序会设置基本 url(即 ),但如果您希望返回该值,则更改 GetPageUrls 来合并它显然是微不足道的。

最后,如果您想在其他地方使用它,则可以直接将其转换为扩展方法。

Your question asks how to replace your hardcoded pageUris variable:

var pageUris = new[] {
    
quot;{navigationManager.BaseUri}somepage/add",
    
quot;{navigationManager.BaseUri}somepage/remove"
};

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:

private string[] GetPageUrls()
{
    return GetType().GetCustomAttributes<RouteAttribute>().Select(x => x.Template).ToArray();
}

This will produce one element for each @page directive you have, containing the values:

"/somepage/add", "/somepage/remove"

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 change GetPageUrls to incorporate that as well.

Finally, if you want to use this elsewhere, it should be straightforward to convert this into an extension method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文