cloudscribe/cloudscribe.Web.Navigation

FindByKey function returns wrong url

Closed this issue · 3 comments

Hi,

I'm trying to get an url in a view from the tree. The node is correct but the url is strange.

@inject NavigationTreeBuilderService TreeBuilder

@{
    var rootNode = await TreeBuilder.GetTree();
    var url = rootNode.FindByKey("FooBar")?.Value?.ResolveUrl();
}

I think the url is now generated by the cloudscribe.Web.Navigation.NavigationNodeExtensions.ResolveUrl function.
This function is just placing some string after each other. If I understand everything correctly, the real function to get an url is cloudscribe.Web.Navigation.NavigationViewModel.AdjustUrl. Is het possible to move the AdjustUrl function so it can be used instead of the ResolveUrl function? Or an optional UrlHelper to the ResolveUrl function?

Thanks for bringing up that method, it was never implemented correctly and I should have removed it long ago.
I would like to do away with that parameterless ResolveUrl extension method altogether. For now I have marked it obsolete and added an overload that requires an IUrlHelper.
Just published nuget version 2.0.6

@joeaudette The method is working great but only for methods without any parameters. I have a NewsArticlesNavigationTreeBuilder and I generate the url there. That url isn't used now. It was in previous version. I think it is because this line is at the bottom if (string.IsNullOrEmpty(urlToUse)) { return node.Url; }. I think it should go up just as the previous version.

Or do you have a better approach for these parameters? My NewsArticlesNavigationTreeBuilder looks like this:

private async Task<TreeNode<NavigationNode>> BuildTreeInternal(NavigationTreeBuilderService service)
{
    var urlHelper = urlHelperFactory.GetUrlHelper(actionContextAccesor.ActionContext);

    var rootNav = new NavigationNode
    {
        Key = SiteMapsHelper.NieuwsKey,
        Text = "Nieuws",
        Controller = "News",
        Action = "Index",
        ComponentVisibility = NamedNavigationFilters.TopNav
    };

    var rootTree = new TreeNode<NavigationNode>(rootNav);

    foreach (var article in await db.NewsArticles.ToListAsync())
    {
        var node = new NavigationNode
        {
            Key = SiteMapsHelper.NieuwsDetailsKey + article.Id,
            Title = article.Title,
            Text = article.Title,
            Controller = "News",
            Action = "Details",
            Url = urlHelper.Action("Details", "News", new {id = article.Id, slug = article.Slug}),
            ComponentVisibility = "newsarticles"
        };

        rootTree.AddChild(node);
    }

    return rootTree;
}

Found the bug. I shouldn't had to add the Contoller and Action property. Without those, it is working fine!