Пример #1
0
        public void CreateRegistry()
        {
            //Find all Controller implementations
            var controllerType = typeof(Controller);
            var controllerImplementations = AppDomain.CurrentDomain.GetAssemblies()
                .SelectMany(s => s.GetTypes())
                .Where(p => controllerType.IsAssignableFrom(p));

            foreach (var controller in controllerImplementations)
            {
                var areaAttribute = controller.GetAttribute<AreaAttribute>();
                var areaName = areaAttribute?.RouteValue;

                var controllerName = controller.Name.Replace("Controller", "");

                var actionResultType = typeof(IActionResult);

                //TODO change first of group to find HTTPGet then first?
                foreach (var controllerAction in controller.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance)
                    .Where(m => actionResultType.IsAssignableFrom(m.ReturnParameter?.ParameterType))
                    .GroupBy(m => m.Name)
                    .Select(g => g.First()))
                {
                    var key = GetRouteKey(areaName, controllerName, controllerAction.Name);

                    var routeAttribute = controllerAction.GetCustomAttribute<RouteAttribute>();
                    var parentAttribute = controllerAction.GetCustomAttribute<ParentAttribute>();

                    var parentKey = parentAttribute?.Key;

                    if (parentKey.IsNotBlank())
                    {
                        //If only the action has been specified, add the current controller to the key
                        if (parentKey.Split('.').Count() == 1)
                        {
                            parentKey = $"{controllerName}.{parentKey}";
                        }

                        //If the area has not been specified, use the current area- if defined
                        if (parentKey.Split('.').Count() == 2 && areaName.IsNotBlank())
                        {
                            parentKey = $"{areaName}.{parentKey}";
                        }
                    }

                    var breadcrumbAction = new BreadcrumbAction
                    {
                        Key = key,
                        Route = routeAttribute?.Template,
                        Title = GetPageTitle(controllerAction, controller),
                        ParentKey = parentKey
                    };

                    Items.Add(breadcrumbAction);
                }
            }
        }
Пример #2
0
        private MenuItem BreadcrumbActionToMenuItem(BreadcrumbAction action, RouteData routeData, bool active)
        {
            var displayText = action.Title;
            var url = action.Route;

            foreach (var key in routeData.Values.Keys)
            {
                var keyRegex = new Regex("{" + key + "(.*?)}");
                var value = routeData.Values[key].ToString();

                displayText = keyRegex.Replace(displayText, value);
                url = keyRegex.Replace(url, value);
            }

            return new MenuItem
            {
                Active = active,
                Url = "/" + url,
                DisplayText = displayText
            };
        }