cloudscribe/cloudscribe.Web.Navigation

Null exception in NavigationViewModel.CurrentNode with no startingNodeKey

Closed this issue · 2 comments

pbros commented

When no startingNodeKey is specified in

@await Component.InvokeAsync("Navigation", new { viewName = "Breadcrumb", filterName = NamedNavigationFilters.Breadcrumbs })

and the standard algorithm does not find a node, this code hits a null-exception and does not go to the nodeFinders loop

if (startingNodeKey.Length > 0)
{
    return StartingNode;
}

A simple fix is to change that code to

if(!string.IsNullOrWhiteSpace(startingNodeKey))
{
    return StartingNode;
}

Which is actually what has been implemented in the StartingNode method.

Current workaround is not specify an empty string when invoking the component as such:

@await Component.InvokeAsync("Navigation", new { viewName = "Breadcrumb", filterName = NamedNavigationFilters.Breadcrumbs, startingNodeKey = "" })

I came across this issue with razor pages where the ID is part of a route and not a query parameter.

For example, my route is "/clients/details/abc-111", which does not match my node at the url "/clients/details"

For reference, here is my node-finder

    public class BreadcrumbFindCurrentNode : IFindCurrentNode
    {
        public TreeNode<NavigationNode> FindNode(TreeNode<NavigationNode> rootNode, IUrlHelper urlHelper, string currentUrl, string urlPrefix = "")
        {
            // Try removing last segments of URL, which can be part of the route data
            var trimmedUrl = currentUrl;
            
            while (trimmedUrl.Contains('/'))
            {
                trimmedUrl = trimmedUrl.Substring(0, trimmedUrl.LastIndexOf('/'));
                var currentNode = rootNode.FindByUrl(urlHelper, trimmedUrl, urlPrefix);
                if (currentNode != null)
                    return currentNode;
            }

            return null;
        }
    }

fyi, just published yet another version bump on the nuget. This time I updated our find current node logic to look for matching razor pages.
So you may or may not need a custom IFindCurrentNode with the latest version.
https://www.nuget.org/packages/cloudscribe.Web.Navigation/2.1.9