Exemplo n.º 1
0
        public BreadcrumbModel CreateBreadcrumb(Controller currentController, ActionExecutingContext filterContext)
        {
            var result = new BreadcrumbModel();

            var controllerType = currentController.GetType();
            var actionName = filterContext.RouteData.Values["action"].ToString();

            var routeKey = GetRouteKey(controllerType, actionName);

            //Add the current route
            var currrentAction = GetItemByKey(routeKey);

            if (currrentAction == null)
                return result;

            result.Items.Add(BreadcrumbActionToMenuItem(currrentAction, filterContext.RouteData, true));

            while (currrentAction?.ParentKey.IsNotBlank() ?? false)
            {
                currrentAction = GetItemByKey(currrentAction.ParentKey);

                //TODO improve, there is no need to go to the next loop
                if (currrentAction == null)
                    continue;

                var ancestorMenuItem = BreadcrumbActionToMenuItem(currrentAction, filterContext.RouteData, false);

                result.Items.Insert(0, ancestorMenuItem);
            }

            return result;
        }
Exemplo n.º 2
0
        public string GetPageTitle(Controller controller, ActionExecutingContext filterContext)
        {
            //Default Page title
            var pageTitle = filterContext.ActionDescriptor.Name.ToDisplayText();

            var actionValue = filterContext.RouteData.Values["action"];

            //Try to get the action name, default to Index
            var actionName = actionValue != null && actionValue.ToString().IsNotBlank()
                ? actionValue.ToString()
                : "Index";

            var controllerType = controller.GetType();
            var actionMember = controllerType.GetMethods().FirstOrDefault(m => m.Name == actionName);

            //Find any custom `TitleAttribute` values
            var titleAttributes = actionMember?.GetCustomAttributes(typeof(TitleAttribute), true);

            if (titleAttributes?.Any() ?? false)
            {
                var titleAttribute = (TitleAttribute)titleAttributes.First();
                if (titleAttribute != null)
                    pageTitle = titleAttribute.PageTitle;
            }
            else if (actionName == "Index")
            {
                //If there is no attribute and the default Index action
                //is in use, use the Controller name as the page title
                pageTitle = controller.GetType().Name.Replace("Controller", "").ToDisplayText();
            }

            //Inject any route data into the page title
            foreach (var routeData in filterContext.RouteData.Values)
            {
                pageTitle = pageTitle.Replace("{" + routeData.Key + "}", routeData.Value.ToString());
            }

            return pageTitle;
        }