|
| 1 | +using Microsoft.AspNetCore.Mvc; |
| 2 | +using Microsoft.AspNetCore.Mvc.Rendering; |
| 3 | + |
| 4 | +namespace Mwh.Sample.Web.Helpers; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Helper class for generating breadcrumb navigation |
| 8 | +/// </summary> |
| 9 | +public static class BreadcrumbHelper |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Generate breadcrumbs based on the current route |
| 13 | + /// </summary> |
| 14 | + public static List<(string Name, string Url)> GenerateBreadcrumbs(ViewContext viewContext) |
| 15 | + { |
| 16 | + var breadcrumbs = new List<(string Name, string Url)>(); |
| 17 | + |
| 18 | + // Always start with Home |
| 19 | + breadcrumbs.Add(("Home", "/")); |
| 20 | + |
| 21 | + var areaName = viewContext.RouteData.Values["area"]?.ToString(); |
| 22 | + var controllerName = viewContext.RouteData.Values["controller"]?.ToString(); |
| 23 | + var actionName = viewContext.RouteData.Values["action"]?.ToString(); |
| 24 | + var idValue = viewContext.RouteData.Values["id"]?.ToString(); |
| 25 | + |
| 26 | + // Skip if we're already on the home page |
| 27 | + if (string.IsNullOrEmpty(controllerName) || controllerName.Equals("Home", StringComparison.OrdinalIgnoreCase)) |
| 28 | + { |
| 29 | + if (string.IsNullOrEmpty(actionName) || actionName.Equals("Index", StringComparison.OrdinalIgnoreCase)) |
| 30 | + { |
| 31 | + return breadcrumbs; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + // Handle different controller patterns |
| 36 | + switch (controllerName?.ToLower()) |
| 37 | + { |
| 38 | + case "mvcemployee": |
| 39 | + breadcrumbs.Add(("Employee Management", "/MvcEmployee")); |
| 40 | + AddActionBreadcrumb(breadcrumbs, actionName, idValue, "/MvcEmployee"); |
| 41 | + break; |
| 42 | + |
| 43 | + case "employee": |
| 44 | + breadcrumbs.Add(("Employee AJAX", "/Employee")); |
| 45 | + AddActionBreadcrumb(breadcrumbs, actionName, idValue, "/Employee"); |
| 46 | + break; |
| 47 | + |
| 48 | + case "employeesinglepage": |
| 49 | + breadcrumbs.Add(("Single Page App", "/EmployeeSinglePage")); |
| 50 | + AddActionBreadcrumb(breadcrumbs, actionName, idValue, "/EmployeeSinglePage"); |
| 51 | + break; |
| 52 | + |
| 53 | + case "employeepivot": |
| 54 | + breadcrumbs.Add(("Employee Pivot", "/EmployeePivot")); |
| 55 | + AddActionBreadcrumb(breadcrumbs, actionName, idValue, "/EmployeePivot"); |
| 56 | + break; |
| 57 | + |
| 58 | + default: |
| 59 | + // Handle other controllers generically |
| 60 | + if (!string.IsNullOrEmpty(controllerName)) |
| 61 | + { |
| 62 | + var controllerDisplayName = FormatControllerName(controllerName); |
| 63 | + breadcrumbs.Add((controllerDisplayName, $"/{controllerName}")); |
| 64 | + AddActionBreadcrumb(breadcrumbs, actionName, idValue, $"/{controllerName}"); |
| 65 | + } |
| 66 | + break; |
| 67 | + } |
| 68 | + |
| 69 | + // Handle Razor Pages |
| 70 | + if (viewContext.ActionDescriptor.DisplayName?.Contains("Pages") == true || |
| 71 | + viewContext.HttpContext.Request.Path.Value?.Contains("/EmployeeRazor") == true) |
| 72 | + { |
| 73 | + breadcrumbs.Clear(); |
| 74 | + breadcrumbs.Add(("Home", "/")); |
| 75 | + breadcrumbs.Add(("Employee Razor Pages", "/EmployeeRazor")); |
| 76 | + |
| 77 | + var pageName = viewContext.HttpContext.Request.Path.Value?.Split('/').LastOrDefault(); |
| 78 | + if (!string.IsNullOrEmpty(pageName) && !pageName.Equals("EmployeeRazor", StringComparison.OrdinalIgnoreCase)) |
| 79 | + { |
| 80 | + AddPageBreadcrumb(breadcrumbs, pageName, idValue); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return breadcrumbs; |
| 85 | + } |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// Add action-specific breadcrumb |
| 89 | + /// </summary> |
| 90 | + private static void AddActionBreadcrumb(List<(string Name, string Url)> breadcrumbs, string? actionName, string? idValue, string basePath) |
| 91 | + { |
| 92 | + if (string.IsNullOrEmpty(actionName) || actionName.Equals("Index", StringComparison.OrdinalIgnoreCase)) |
| 93 | + return; |
| 94 | + |
| 95 | + switch (actionName.ToLower()) |
| 96 | + { |
| 97 | + case "create": |
| 98 | + breadcrumbs.Add(("Create Employee", $"{basePath}/Create")); |
| 99 | + break; |
| 100 | + |
| 101 | + case "edit": |
| 102 | + breadcrumbs.Add(("Edit Employee", string.IsNullOrEmpty(idValue) ? $"{basePath}/Edit" : $"{basePath}/Edit/{idValue}")); |
| 103 | + break; |
| 104 | + |
| 105 | + case "details": |
| 106 | + breadcrumbs.Add(("Employee Details", string.IsNullOrEmpty(idValue) ? $"{basePath}/Details" : $"{basePath}/Details/{idValue}")); |
| 107 | + break; |
| 108 | + |
| 109 | + case "delete": |
| 110 | + breadcrumbs.Add(("Delete Employee", string.IsNullOrEmpty(idValue) ? $"{basePath}/Delete" : $"{basePath}/Delete/{idValue}")); |
| 111 | + break; |
| 112 | + |
| 113 | + default: |
| 114 | + var actionDisplayName = FormatActionName(actionName); |
| 115 | + breadcrumbs.Add((actionDisplayName, $"{basePath}/{actionName}")); |
| 116 | + break; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /// <summary> |
| 121 | + /// Add page-specific breadcrumb for Razor Pages |
| 122 | + /// </summary> |
| 123 | + private static void AddPageBreadcrumb(List<(string Name, string Url)> breadcrumbs, string pageName, string? idValue) |
| 124 | + { |
| 125 | + switch (pageName.ToLower()) |
| 126 | + { |
| 127 | + case "create": |
| 128 | + breadcrumbs.Add(("Create Employee", "/EmployeeRazor/Create")); |
| 129 | + break; |
| 130 | + |
| 131 | + case "edit": |
| 132 | + breadcrumbs.Add(("Edit Employee", string.IsNullOrEmpty(idValue) ? "/EmployeeRazor/Edit" : $"/EmployeeRazor/Edit/{idValue}")); |
| 133 | + break; |
| 134 | + |
| 135 | + case "details": |
| 136 | + breadcrumbs.Add(("Employee Details", string.IsNullOrEmpty(idValue) ? "/EmployeeRazor/Details" : $"/EmployeeRazor/Details/{idValue}")); |
| 137 | + break; |
| 138 | + |
| 139 | + case "delete": |
| 140 | + breadcrumbs.Add(("Delete Employee", string.IsNullOrEmpty(idValue) ? "/EmployeeRazor/Delete" : $"/EmployeeRazor/Delete/{idValue}")); |
| 141 | + break; |
| 142 | + |
| 143 | + default: |
| 144 | + var pageDisplayName = FormatPageName(pageName); |
| 145 | + breadcrumbs.Add((pageDisplayName, $"/EmployeeRazor/{pageName}")); |
| 146 | + break; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + /// <summary> |
| 151 | + /// Format controller name for display |
| 152 | + /// </summary> |
| 153 | + private static string FormatControllerName(string controllerName) |
| 154 | + { |
| 155 | + return controllerName switch |
| 156 | + { |
| 157 | + "MvcEmployee" => "MVC Employee", |
| 158 | + "EmployeeSinglePage" => "Single Page App", |
| 159 | + "EmployeePivot" => "Employee Pivot", |
| 160 | + _ => controllerName |
| 161 | + }; |
| 162 | + } |
| 163 | + |
| 164 | + /// <summary> |
| 165 | + /// Format action name for display |
| 166 | + /// </summary> |
| 167 | + private static string FormatActionName(string actionName) |
| 168 | + { |
| 169 | + return actionName switch |
| 170 | + { |
| 171 | + "Create" => "Create", |
| 172 | + "Edit" => "Edit", |
| 173 | + "Details" => "Details", |
| 174 | + "Delete" => "Delete", |
| 175 | + _ => actionName |
| 176 | + }; |
| 177 | + } |
| 178 | + |
| 179 | + /// <summary> |
| 180 | + /// Format page name for display |
| 181 | + /// </summary> |
| 182 | + private static string FormatPageName(string pageName) |
| 183 | + { |
| 184 | + return pageName switch |
| 185 | + { |
| 186 | + "Create" => "Create", |
| 187 | + "Edit" => "Edit", |
| 188 | + "Details" => "Details", |
| 189 | + "Delete" => "Delete", |
| 190 | + _ => pageName |
| 191 | + }; |
| 192 | + } |
| 193 | +} |
0 commit comments