8
I’m writing a system on Asp.net-mvc And the idea is that there’s a Breadcrumb on every canvas. With no ideas for a good algorithm, I created a Helper that returns a list of objects according to the path accessed. The file is reproduced below:
using System;
using System.Collections.Generic;
using MeuProjeto.ViewModels;
namespace MeuProjeto.Helpers
{
public static class BreadcrumbHelper
{
private static readonly Dictionary<String, List<BreadcrumbViewModel>> Breadcrumbs =
new Dictionary<string, List<BreadcrumbViewModel>>
{
{ "/Pessoas",
new List<BreadcrumbViewModel> {
new BreadcrumbViewModel { Title = "Gestão de Colaboradores" },
new BreadcrumbViewModel { Title = "Lista de Pessoas" }
}
},
{ "/Pessoas/Index",
new List<BreadcrumbViewModel> {
new BreadcrumbViewModel { Title = "Gestão de Colaboradores" },
new BreadcrumbViewModel { Title = "Lista de Pessoas" }
}
},
{ "/Pessoas/Create",
new List<BreadcrumbViewModel> {
new BreadcrumbViewModel { Title = "Gestão de Colaboradores" },
new BreadcrumbViewModel { Title = "Dados Pessoais" }
}
},
{ "/Pessoas/Edit",
new List<BreadcrumbViewModel> {
new BreadcrumbViewModel { Title = "Gestão de Colaboradores" },
new BreadcrumbViewModel { Title = "Dados Pessoais" }
}
}
};
public static IEnumerable<BreadcrumbViewModel> GetBreadcrumbs(String url)
{
return Breadcrumbs[url];
}
}
}
For me to get the Breadcrumbs list according to the route I’m on, it’s simple: I put in the common controller code the following:
[ChildActionOnly]
public ActionResult Breadcrumb()
{
return PartialView(BreadcrumbHelper.GetBreadcrumbs(Request.Path));
}
And in View Shared/_Layout.cshtml
the following:
<body>
<!-- Page background -->
<div id="page-background-gradient">
</div>
<div id="page-background-glare">
</div>
<!-- /Page background -->
@Html.Partial("_SectionHeader")
@Html.Partial("_MainNavigation")
@Html.Action("Showcase")
@Html.Action("Breadcrumb")
<section id="content" class="row">
@RenderBody()
</section>
@Html.Partial("_SectionFooter")
@Scripts.Render("~/bundles/jqueryui")
@Scripts.Render("~/bundles/jquery.datatables")
@RenderSection("scripts", required: false)
</body>
</html>
The result in the View looks like this for http://localhost:12345/Pessoas
:
Home > Employee Management > People List
That works well, but I see two problems:
- The schema obviously explodes if I create a new controller and do not add the new routes in the
BreadcrumbHelper
; - It is easy to see that the organization of the solution is prolific and redundant. I wanted something simpler and more performative, but I couldn’t think of anything better.
Ideas?
If everything in the same controller has the same parent in the hierarchy (as in the case of "Employee Management"), you could keep this part as controller property, right? It would be more Coupled, but less repetitive.
– bfavaretto
It’s an idea, but I wanted to leave as much stuff inside
BreadcrumbHelper
.– Leonel Sanches da Silva