Breadcrumb.Plugin

Assembly: Breadcrumb.Plugin

Description

Breadcrumbs provide an additional form of navigational aid to users, they can help to inform the user of their current position within the site and provide an additional form of navigation.

The Breadcrumb plugin makes it easy to create breadcrumbs within a web application, it works in two ways:

  • Attributes are placed on controller methods where a Breadcrumb is required.
  • Progmatically create Breadcrumb items and their heirerarchy whilst processing requests.

However they are used, they follow a very simple form, a Breadcrumb can function as both a parent and child to another Breadcrumb item. If attribute breadcrumbs are used then the parent of the Breadcrumb is also listed, this allows the middleware to build up the Breadcrumb heirerarchy which is then inserted into the temp data for the context being processed.

Once the Breadcrumb data has been provided to the controller, it can then be extracted and placed within the view model for display within the view.

Localization

The Breadcrumb plugin fully supports localizatin, provided that the Localization.Plugin module is also loaded. Whilst building the Breadcrumb heirerarchy for the request, the name of the Breadcrumb is translated to the current culture specified by the user.

Caching

Page load time is a very important aspect of any website, to ensure that the Breadcrumb is delivered in a timely manner the requests are cached for a minimum of 20 minutes after the first request.

Attribute Based Breadcrumbs

The following example, taken from the CompanyPlugin demonstrates adding a Breadcrumb to the view which shows the cookie policy.

[Breadcrumb(nameof(Languages.LanguageStrings.Cookies))]
public IActionResult Cookies()
{
    // code removed for brevity
}

When viewing the cookie page, the Breadcrumb presented would look like:

Home / Cookies

From the shopping cart plugin, the following example show building a heirerarchy of breadcrumbs.

[HttpGet]
[Breadcrumb(nameof(Languages.LanguageStrings.ShoppingCart))]
public IActionResult Index()
{
    // code removed for brevity
}

[HttpGet]
[Breadcrumb(nameof(Shipping), nameof(CartController), nameof(Index))]
public IActionResult Shipping()
{
    // code removed for brevity
}

[HttpPost]
[Breadcrumb(nameof(Checkout), nameof(CartController), nameof(Shipping))]
public IActionResult Checkout(intshippingId)
{
    // code removed for brevity
}

In the example above, the Index method of the shopping cart has a simple breadcrumb, with no parent, this will show

Home / Shopping Cart

The next step in the shopping cart is to select the shipping Address (if applicable). The Breadcrumb for this action method has a name of shipping and the parent points to the Index action method. The Breadcrumb for this would look like

Home / Shopping Cart / Shipping

The last example above is the checkout action method, this is the logical step after the delivery Address is. The Breadcrumb has a parent pointing to the Shipping action method and would look like

Home / Shopping Cart / Shipping / Checkout

If the Product does not require shipping we could manually remove the shipping Breadcrumb from the list:

if (!cartDetail.RequiresShipping)
    model.Breadcrumbs.RemoveAt(2);

There are more complex cases when working with dynamic data, for this scenario you can use IBreadcrumbService interface. This interface which is available using dependency injection has methods which allows for breadcrumbs to be added dynamically when initialising and will then operate like a standard attribute based Breadcrumb item. The following code taken from the SystemAdmin.Plugin demonstrates how the breadcrumbs are generated for dynamic menu items only known at runtime:

private void RegisterBreadcrumbs(in IBreadcrumbService breadcrumbService, 
    in ISystemAdminHelperService systemAdminHelper)
{
    string parentRoute = "/SystemAdmin";
    breadcrumbService.AddBreadcrumb(nameof(Languages.LanguageStrings.SystemAdmin), parentRoutefalse);
 
    foreach (SystemAdminMainMenu item in systemAdminHelper.GetSystemAdminMainMenu())
    {
        string route = $"/SystemAdmin/Index/{item.UniqueId}";
        breadcrumbService.AddBreadcrumb(item.Name, routeparentRoutefalse);
 
        foreach (SystemAdminSubMenu childItem in item.ChildMenuItems)
        {
            switch (childItem.MenuType())
            {
                case Enums.SystemAdminMenuType.Grid:
                    breadcrumbService.AddBreadcrumb(childItem.Name(), $"/SystemAdmin/Grid/{childItem.UniqueId}"routefalse);
                    break;
                case Enums.SystemAdminMenuType.Text:
                    breadcrumbService.AddBreadcrumb(childItem.Name(), $"/SystemAdmin/Text/{childItem.UniqueId}"routefalse);
                    break;
                case Enums.SystemAdminMenuType.PartialView:
                    breadcrumbService.AddBreadcrumb(childItem.Name(), $"/SystemAdmin/View/{childItem.UniqueId}"routefalse);
                    break;
                case Enums.SystemAdminMenuType.Map:
                    breadcrumbService.AddBreadcrumb(childItem.Name(), $"/SystemAdmin/Map/{childItem.UniqueId}"routefalse);
                    break;
                case Enums.SystemAdminMenuType.FormattedText:
                    breadcrumbService.AddBreadcrumb(childItem.Name(), $"/SystemAdmin/TextEx/{childItem.UniqueId}"routefalse);
                    break;
            }
 
        }
    }
}

Retreiving Breadcrumb Data

The Breadcrumb data that is generated is inserted into the HttpContext.Items for the request and can be easily retrieved by ensuring that the breadcrumbs exists and returning them. BaseController which is a class that any controller can inherit from, contains a method to obtain the Breadcrumb data:

protected List<BreadcrumbItemGetBreadcrumbs()
{
    if (HttpContext.Items.ContainsKey(Constants.Breadcrumbs))
    {
        return (List<BreadcrumbItem>)HttpContext.Items[Constants.Breadcrumbs];
    }
 
    return new List<BreadcrumbItem>();
}

The BaseModel class is a simple class which any view model can inherit from and contains two methods which can be used to retrieve Breadcrumb data, the overloaded methods are both called BreadcrumbData .

As can be seen the Breadcrumb plugin makes building a Breadcrumb heirerarchy very simple indeed, but adds the required flexibility to ensure cope with a vast variety of situations.