I'm writing a system on asp. net-mvc and the idea is that on the screens there is a Breadcrumb on all screens. Without ideas for a good algorithm, I booted a Helper that returns a list of objects according to the route 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];
}
}
}
To get the list of Breadcrumbs according to the route I'm in, it's simple: I put the following code in the common controller code:
[ChildActionOnly]
public ActionResult Breadcrumb()
{
return PartialView(BreadcrumbHelper.GetBreadcrumbs(Request.Path));
}
And in View% with% as follows:
<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 View looks like this for Shared/_Layout.cshtml
:
Home > Employee Management > List of People
This works fine, but I see two problems:
- The scheme obviously explodes if I create a new controller and do not add the new routes in
http://localhost:12345/Pessoas
; - It's easy to see that the organization of the solution is redundant and redundant. I wanted something simpler and more performative, but I could not think of anything better.
Ideas?