Beispiel #1
0
        /// <summary>
        /// Adds a sequence of <see cref="Breadcrumb"/> objects to the specified <paramref name="viewData"/>.
        /// </summary>
        /// <typeparam name="T">The type of the model to retrieve breadcrumb labels from.</typeparam>
        /// <param name="viewData">The <see cref="ViewDataDictionary"/> to extend.</param>
        /// <param name="controller">The controller to resolve all public methods with <see cref="IActionResult"/> as return type from.</param>
        /// <param name="model">The model to retrieve custom breadcrumb labels from.</param>
        /// <param name="initializer">The function delegate that will initialize labels from the spcified <paramref name="model"/>.</param>
        public static void AddBreadcrumbs <T>(this ViewDataDictionary viewData, Controller controller, T model, Func <T, IEnumerable <string> > initializer)
        {
            var list          = new List <Breadcrumb>();
            var ct            = controller.GetType();
            var actions       = ct.GetMethods(ReflectionUtility.BindingInstancePublic).Where(mi => mi.ReturnType == typeof(IActionResult)).ToList();
            var labelInvokers = initializer(model).ToList();

            for (int i = 0; i < actions.Count; i++)
            {
                var bc = new Breadcrumb()
                {
                    ActionName     = actions[i].Name,
                    ControllerName = controller.RouteData.Values["controller"] as string,
                    Label          = labelInvokers[i]
                };
                list.Add(bc);
            }

            viewData.AddOrUpdate(BreadcrumbKey, list);
        }