I found a nice answer on creating breadcrumbs for this project here:
How can dynamic breadcrumbs be achieved with ASP.net MVC?
The modified code that I am using is here:
public static string BuildBreadcrumbNavigation(this HtmlHelper helper)
{
var result = string.Empty;
var controllerName = helper.ViewContext.RouteData.Values["controller"].ToString();
if ((controllerName != "Home") && (controllerName != "Account"))
{
var htmlLink = helper.ActionLink(
linkText: "Home",
actionName: "/",
controllerName: "Home").ToHtmlString();
var sb = new StringBuilder($"<ol class='breadcrumb'><li>{htmlLink}</li>");
var controllerLink = helper.ActionLink(
linkText: controllerName,
actionName: "/",
controllerName: controllerName);
sb.Append($"<li>{controllerLink}</li>");
var actionName = helper.ViewContext.RouteData.Values["action"].ToString();
var actionLink = helper.ActionLink(
linkText: actionName,
actionName: actionName,
controllerName: controllerName);
sb.Append($"<li>{actionLink}</li>");
result = sb.Append("</ol>").ToString();
}
return result;
}
The webpage I want to implement it on has a navigation menu:
- Services
- Parts
- Bulletins
- Publications
- Warranty
- Sales
- Buyers
- Image Resources
- Videos
For something like Home > Services > Parts
, there is a controller called ServiceController.cs and a model called PartsInformation.cs.
The links for Home
and Parts
work fine, but there is nothing to display for the intermediate Services
because it is a menu item only. Clicking Services
attempts to redirect here:
https://localhost:44383/Services/
What should be done here?
Should I remove the link for Services
and leave the text, or should I have Services
route to Home
?
I would like to route the root navigation menu items back to Home
, but I don't understand enough about this ActionLink.