Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a dynamic breadcrumb navigation for .net core razor pages?

Im trying to create a breadcrumb navigation, i.e.

Home > Contact

I have found this answer - link

But it seams to be for MVC rather than razor pages. And i cant seam to find anything that works with razor pages.

Thanks in advance :)

Answer

As my answer was deleted by a moderator, here was my soloution -

To solve this I found this project - https://github.com/zHaytam/SmartBreadcrumbs

Very easy to use and worked flawlessly

like image 327
IeuanW Avatar asked Nov 08 '22 02:11

IeuanW


1 Answers

I had this problem and solved it in this way:

At first I made a razor component for Breadcrumb like below:

 <nav aria-label="breadcrumb" style="background-color: #f2f2f3">
    <div class="mt-max-size">
        <ol class="breadcrumb">
            @foreach (var link in Links.OrderBy(x => x.OrderIndex))
            {
                if (link.IsActive)
                {
                    <li class="breadcrumb-item active mt-bread-item" aria-current="page">@link.Title</li>
                }
                else
                {
                    <li class="breadcrumb-item"><a class="mt-bread-item-link" href="@link.Address">@link.Title</a></li>
                }
            }
        </ol>
    </div>
</nav>
    @code{
        [Parameter]
        public List<Model.BreadcrumbLink> Links { get; set; }
        public Breadcrumb()
        {
    
        }
            protected override Task OnParametersSetAsync() => base.OnParametersSetAsync();
    }

And then I have used this component in my razor layout like below:

@inherits LayoutComponentBase
@inject NavigationManager NavigationManager

<Breadcrumb Links="@breadcrumbLinks" />

@code{
    private string currentUrl;
protected override void OnParametersSet() {
        breadcrumbLinks = new List<Model.BreadcrumbLink>();
        currentUrl = NavigationManager.Uri;
        var myUrl = currentUrl.Replace(NavigationManager.BaseUri, "");
        breadcrumbLinks.Add(new Model.BreadcrumbLink
        {
            Address = NavigationManager.BaseUri,
            IsActive = NavigationManager.Uri == NavigationManager.BaseUri,
            OrderIndex = 1,
            Title = @Localizer["Home".ToLower()]
        });
        var path = myUrl.Split('/');
        var count = 1;
        
        foreach (var link in path)
        {
            if (link == "") continue;
            count++;
            var lastLink = breadcrumbLinks.Last();
            breadcrumbLinks.Add(new Model.BreadcrumbLink
            {
                Address = $"{lastLink.Address}/{link}",
                IsActive = link == path.Last(),
                OrderIndex = count,
                Title = link
            });
        }
        base.OnParametersSet();
    }
}

Note: To pass the data I Have created a model: Model.BreadcrumbLink. You can see its definition below :

public class BreadcrumbLink
    {
        public int OrderIndex { get; set; }
        public string Address { get; set; }
        public string Title { get; set; }
        public bool IsActive { get; set; }
    }

I hope it be helpful !!

like image 168
Amirhossein Yari Avatar answered Nov 14 '22 07:11

Amirhossein Yari