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 !!