I am trying to figure what asp.net core 6 razor view I have come from so I know what to put in my breadrumb trail.
I have the following html extension that builds the breadcrumb trail -
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using System;
using System.IO;
using System.Text.Json;
using YellowFeverPortal.Web.Models;
namespace YellowFeverPortal.Web.Extensions
{
public static class HtmlExtensions
{
private static readonly HtmlContentBuilder _emptyBuilder = new HtmlContentBuilder();
public static Rootobject _ob;
public static string _vaccineClinicId;
public static IHtmlContent BuildBreadcrumbNavigation(this IHtmlHelper helper)
{
string jsonData = File.ReadAllText("BreadcrumbMap.json");
_ob = JsonSerializer.Deserialize<Rootobject>(jsonData);
if (helper.ViewContext.RouteData.Values["controller"] is null)
{
return _emptyBuilder;
}
if (helper.ViewContext.RouteData.Values["controller"].ToString() == "Home" ||
helper.ViewContext.RouteData.Values["controller"].ToString() == "Account")
{
return _emptyBuilder;
}
string controllerName = helper.ViewContext.RouteData.Values["controller"].ToString();
string actionName = helper.ViewContext.RouteData.Values["action"].ToString();
string theId = string.Empty;
if (helper.ViewContext.RouteData.Values["id"] is not null)
{
theId = helper.ViewContext.RouteData.Values["id"].ToString();
}
var breadcrumb = new HtmlContentBuilder().AppendHtml("<ol class='breadcrumb'>");
if (!helper.ViewContext.HttpContext.User.IsInRole("VCStaff"))
{
breadcrumb = breadcrumb.AppendHtml(Build(helper, _ob.YFDashboard.Name.TitleSize(), _ob.YFDashboard.Action, _ob.YFDashboard.Controller, theId));
if (actionName.Equals(_ob.YFVCList.Action, StringComparison.OrdinalIgnoreCase))
{
breadcrumb = breadcrumb.AppendHtml(Build(helper, _ob.YFVCList.Name.TitleSize(), _ob.YFVCList.Action, _ob.YFVCList.Controller, theId));
}
}
// New YFVC Trail
if (actionName.Equals(_ob.NewYFVC.Action, StringComparison.OrdinalIgnoreCase))
{
breadcrumb = breadcrumb.AppendHtml(Build(helper, _ob.NewYFVC.Name.TitleSize(), _ob.NewYFVC.Action, _ob.NewYFVC.Controller, theId));
}
// Annual Figures List
if (controllerName.Equals(_ob.ListAnnualFigures.Controller, StringComparison.OrdinalIgnoreCase))
{
if (actionName.Equals(_ob.ListAnnualFigures.Action, StringComparison.OrdinalIgnoreCase))
{
breadcrumb = breadcrumb.AppendHtml(Build(helper, _ob.ListAnnualFigures.Name.TitleSize(), _ob.ListAnnualFigures.Action, _ob.ListAnnualFigures.Controller, theId));
}
}
// YFVC Dashboard Trail
if (controllerName.Equals(_ob.YFVC.Controller, StringComparison.OrdinalIgnoreCase) || controllerName.Equals(_ob.YFVCDetails.Controller, StringComparison.OrdinalIgnoreCase))
{
if (controllerName.Equals(_ob.YFVC.Controller, StringComparison.OrdinalIgnoreCase))
{
breadcrumb = breadcrumb.AppendHtml(Build(helper, _ob.YFVC.Name.TitleSize(), _ob.YFVC.Action, _ob.YFVC.Controller, theId));
}
if (controllerName.Equals(_ob.YFVCDetails.Controller, StringComparison.OrdinalIgnoreCase))
{
if (actionName.Equals(_ob.YFVCDetails.Action, StringComparison.OrdinalIgnoreCase))
{
breadcrumb = breadcrumb.AppendHtml(Build(helper, _ob.YFVC.Name.TitleSize(), _ob.YFVC.Action, _ob.YFVC.Controller, theId));
breadcrumb = breadcrumb.AppendHtml(Build(helper, _ob.YFVCDetails.Name.TitleSize(), _ob.YFVCDetails.Action, _ob.YFVCDetails.Controller, theId));
}
}
}
// Desination trail
if (controllerName.Equals(_ob.Designations.Controller, StringComparison.OrdinalIgnoreCase))
{
if (!actionName.Equals(_ob.Editdesignation.Action, StringComparison.OrdinalIgnoreCase))
{
_vaccineClinicId = theId;
}
breadcrumb = breadcrumb.AppendHtml(Build(helper, _ob.YFVC.Name.TitleSize(), _ob.YFVC.Action, _ob.YFVC.Controller, _vaccineClinicId));
breadcrumb = breadcrumb.AppendHtml(Build(helper, _ob.Designations.Name.TitleSize(), _ob.Designations.Action, _ob.Designations.Controller, _vaccineClinicId));
if (actionName.Equals(_ob.CurrentDesignation.Action, StringComparison.OrdinalIgnoreCase))
{
breadcrumb = breadcrumb.AppendHtml(Build(helper, _ob.CurrentDesignation.Name.TitleSize(), _ob.CurrentDesignation.Action, _ob.CurrentDesignation.Controller, _vaccineClinicId));
}
if (actionName.Equals(_ob.Editdesignation.Action, StringComparison.OrdinalIgnoreCase))
{
breadcrumb = breadcrumb.AppendHtml(Build(helper, _ob.Editdesignation.Name.TitleSize(), _ob.Editdesignation.Action, _ob.Editdesignation.Controller, theId));
}
}
//Training Trail
if (controllerName.Equals(_ob.TrainingList.Controller, StringComparison.OrdinalIgnoreCase))
{
if (!actionName.Equals(_ob.TrainingUpdate.Action, StringComparison.OrdinalIgnoreCase))
{
_vaccineClinicId = theId;
}
breadcrumb = breadcrumb.AppendHtml(Build(helper, _ob.YFVC.Name.TitleSize(), _ob.YFVC.Action, _ob.YFVC.Controller, _vaccineClinicId));
breadcrumb = breadcrumb.AppendHtml(Build(helper, _ob.TrainingList.Name.TitleSize(), _ob.TrainingList.Action, _ob.TrainingList.Controller, _vaccineClinicId));
if (actionName.Equals(_ob.TrainingNew.Action, StringComparison.OrdinalIgnoreCase))
{
breadcrumb = breadcrumb.AppendHtml(Build(helper, _ob.TrainingNew.Name.TitleSize(), _ob.TrainingNew.Action, _ob.TrainingNew.Controller, _vaccineClinicId));
}
if (actionName.Equals(_ob.TrainingUpdate.Action, StringComparison.OrdinalIgnoreCase))
{
breadcrumb = breadcrumb.AppendHtml(Build(helper, _ob.TrainingUpdate.Name.TitleSize(), _ob.TrainingUpdate.Action, _ob.TrainingUpdate.Controller, theId));
}
}
//Annual Figures trail
if (controllerName.Equals(_ob.AnnualFiguresList.Controller, StringComparison.OrdinalIgnoreCase))
{
if (!actionName.Equals(_ob.ListAnnualFigures.Action, StringComparison.OrdinalIgnoreCase))
{
if (!actionName.Equals(_ob.AnnualFiguresUpdate.Action, StringComparison.OrdinalIgnoreCase) && !actionName.Equals(_ob.AnnualFigureFormsUpdate.Action, StringComparison.OrdinalIgnoreCase))
{
_vaccineClinicId = theId;
}
breadcrumb = breadcrumb.AppendHtml(Build(helper, _ob.YFVC.Name.TitleSize(), _ob.YFVC.Action, _ob.YFVC.Controller, _vaccineClinicId));
breadcrumb = breadcrumb.AppendHtml(Build(helper, _ob.AnnualFiguresList.Name.TitleSize(), _ob.AnnualFiguresList.Action, _ob.AnnualFiguresList.Controller, _vaccineClinicId));
if (actionName.Equals(_ob.AnnualFiguresNew.Action, StringComparison.OrdinalIgnoreCase))
{
breadcrumb = breadcrumb.AppendHtml(Build(helper, _ob.AnnualFiguresNew.Name.TitleSize(), _ob.AnnualFiguresNew.Action, _ob.AnnualFiguresNew.Controller, _vaccineClinicId));
}
if (actionName.Equals(_ob.AnnualFiguresUpdate.Action, StringComparison.OrdinalIgnoreCase))
{
breadcrumb = breadcrumb.AppendHtml(Build(helper, _ob.AnnualFiguresUpdate.Name.TitleSize(), _ob.AnnualFiguresUpdate.Action, _ob.AnnualFiguresUpdate.Controller, theId));
}
if (actionName.Equals(_ob.AnnualFigureFormsUpdate.Action, StringComparison.OrdinalIgnoreCase))
{
breadcrumb = breadcrumb.AppendHtml(Build(helper, _ob.AnnualFigureFormsUpdate.Name.TitleSize(), _ob.AnnualFigureFormsUpdate.Action, _ob.AnnualFigureFormsUpdate.Controller, theId));
}
}
}
//Audit List Trail
if (controllerName.Equals(_ob.AuditList.Controller, StringComparison.OrdinalIgnoreCase))
{
if (!actionName.Equals(_ob.AuditDetails.Action, StringComparison.OrdinalIgnoreCase))
{
_vaccineClinicId = theId;
}
breadcrumb = breadcrumb.AppendHtml(Build(helper, _ob.YFVC.Name.TitleSize(), _ob.YFVC.Action, _ob.YFVC.Controller, _vaccineClinicId));
breadcrumb = breadcrumb.AppendHtml(Build(helper, _ob.AuditList.Name.TitleSize(), _ob.AuditList.Action, _ob.AuditList.Controller, _vaccineClinicId));
if (actionName.Equals(_ob.AuditDetails.Action, StringComparison.OrdinalIgnoreCase))
{
breadcrumb = breadcrumb.AppendHtml(Build(helper, _ob.AuditDetails.Name.TitleSize(), _ob.AuditDetails.Action, _ob.AuditDetails.Controller, theId));
}
}
if (controllerName.Equals(_ob.Reports.Controller, StringComparison.OrdinalIgnoreCase))
{
breadcrumb = breadcrumb.AppendHtml(Build(helper, _ob.Reports.Name.TitleSize(), "", _ob.Reports.Controller, ""));
}
return breadcrumb.AppendHtml("</ol>");
}
private static IHtmlContentBuilder Build(IHtmlHelper helper, string title, string actionName, string controller, string Id)
{
var breadcrumb = new HtmlContentBuilder()
.AppendHtml("<li class='breadcrumb-item'>")
.AppendHtml(helper.ActionLink(title, actionName, controller, new { id = Id }, null))
.AppendHtml("</li>");
return breadcrumb;
}
}
}
An example of my breadcrumb map -
{
"YFDashboard": {
"Name": "YF Dashboard",
"Controller": "yfdashboard",
"Action": "DisplayDashboard"
},
"YFVCList": {
"Name": "YFVC List",
"Controller": "vaccinationclinic",
"Action": "index"
},
"NewYFVC": {
"Name": "New YFVC",
"Controller": "VaccinationClinic",
"Action": "New"
}
My breadcrumb model -
using System.ComponentModel.DataAnnotations.Schema;
namespace YellowFeverPortal.Web.Models
{
[NotMapped]
public class Rootobject
{
public YFDashboard YFDashboard { get; set; }
public Yfvclist YFVCList { get; set; }
public Newyfvc NewYFVC { get; set; }
public Yfvcdetails YFVCDetails { get; set; }
public YFVC YFVC { get; set; }
public Designations Designations { get; set; }
public Currentdesignation CurrentDesignation { get; set; }
public Editdesignation Editdesignation { get; set; }
public Traininglist TrainingList { get; set; }
public Trainingnew TrainingNew { get; set; }
public Trainingupdate TrainingUpdate { get; set; }
public Annualfigureslist AnnualFiguresList { get; set; }
public Annualfiguresnew AnnualFiguresNew { get; set; }
public Annualfiguresupdate AnnualFiguresUpdate { get; set; }
public AnnualfigureFormsUpdate AnnualFigureFormsUpdate { get; set; }
public Listannualfigures ListAnnualFigures { get; set; }
public Auditlist AuditList { get; set; }
public Auditdetails AuditDetails { get; set; }
public Reports Reports { get; set; }
}
[NotMapped]
public class YFDashboard
{
public string Name { get; set; }
public string Controller { get; set; }
public string Action { get; set; }
}
[NotMapped]
public class Yfvclist
{
public string Name { get; set; }
public string Controller { get; set; }
public string Action { get; set; }
}
[NotMapped]
public class Newyfvc
{
public string Name { get; set; }
public string Controller { get; set; }
public string Action { get; set; }
}
[NotMapped]
public class Yfvcdetails
{
public string Name { get; set; }
public string Controller { get; set; }
pub#
My breadcrumb trail is called from my _Layout.cshtml -
<div id="maincontent" class="container">
<nav aria-label="breadcrumb">
<!-- #region Breadcrumb -->
@Html.BuildBreadcrumbNavigation()
<!-- #endregion -->
</nav>
<main role="main" class="pb-3">
@RenderBody()
</main>
</div>
What I want to know is what razor view I cam from, so that I can build the breadcrumb trail properly. I thought if I could get the breadcrumb html e.g.
<ol class="breadcrumb">
<li class="breadcrumb-item">
<a href="/yfdashboard/DisplayDashboard">YF Dashboard</a>
</li>
<li class="breadcrumb-item">
<a href="/vaccinationclinic">YFVC List</a>
</li>
</ol>
I would know that the YFVCList view is the view that I came from.
Is this at all possible or is there a totally easier way to find what view you came from?
Here is a sample you could follow:
public static class HtmlExtensions
{
// Other code...
public static void StorePreviousView(HttpContext httpContext)
{
httpContext.Session.SetString("PreviousController", httpContext.Request.RouteValues["controller"].ToString());
httpContext.Session.SetString("PreviousAction", httpContext.Request.RouteValues["action"].ToString());
}
public static IHtmlContent BuildBreadcrumbNavigation(this IHtmlHelper helper, HttpContext httpContext)
{
// Other code...
var previousController = httpContext.Session.GetString("PreviousController");
var previousAction = httpContext.Session.GetString("PreviousAction");
// Use previousController and previousAction to determine the previous view
// and adjust breadcrumb trail accordingly.
// Other code...
}
}
Then, in your controller actions, call HtmlExtensions.StorePreviousView(HttpContext)
to store the previous controller and action.