Ejemplo n.º 1
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            UrlHelper url        = new UrlHelper(filterContext.RequestContext);
            var       controller = filterContext.Controller.ToString().Split('.').Last().Replace("Controller", "");
            var       action     = filterContext.ActionDescriptor.ActionName;
            var       area       = (string)filterContext.HttpContext.Request.RequestContext.RouteData.DataTokens["area"] ?? string.Empty;

            SimpleSiteMapModel simpleBreadcrumb = new SimpleSiteMapModel();

            simpleBreadcrumb.Label = label;

            simpleBreadcrumb.URL = url.Action(action, controller, new { Area = area, id = "" });

            var parent = PopulateParent(filterContext, this.parentController, this.parentArea, this.parentAction);

            //If is the first one
            simpleBreadcrumb.SimpleSiteMapControllerParent = (parent.URL == simpleBreadcrumb.URL) ? null : parent;

            var configurations = (SimpleSiteMapControllerConfiguration)filterContext.HttpContext.Application["SimpleSiteMapControllerConfiguration"];

            //var ViewData = filterContext.Controller.ViewData;
            //ViewData[configurations.ViewSimpleSiteMapControllerBreadcrumbName] = simpleBreadcrumb;
            HttpContext.Current.Items.Add(configurations.ViewSimpleSiteMapControllerBreadcrumbName, simpleBreadcrumb);
        }
Ejemplo n.º 2
0
        public SimpleSiteMapModel PopulateParent(ActionExecutingContext filterContext, string parentController, string parentArea, string parentAction)
        {
            if (string.IsNullOrWhiteSpace(parentController))
            {
                return(PopulateParentDefault(filterContext));
            }

            SimpleSiteMapModel simpleSiteMapController = new SimpleSiteMapModel();

            UrlHelper url        = new UrlHelper(filterContext.RequestContext);
            var       controller = parentController;
            var       action     = parentAction;
            var       area       = parentArea ?? string.Empty;

            simpleSiteMapController.URL = url.Action(action, controller, new { Area = area, id = "" });

            var type          = typeof(Controller);
            var selectedClass = AppDomain.CurrentDomain.GetAssemblies()
                                .SelectMany(s => s.GetTypes())
                                .Where(p => type.IsAssignableFrom(p) &&
                                       p.Name == parentController + "Controller" &&
                                       (
                                           (
                                               string.IsNullOrWhiteSpace(parentArea) && p.Namespace.Contains("Areas") == false
                                           ) ||
                                           (
                                               !string.IsNullOrWhiteSpace(parentArea) && p.Namespace.Contains("Areas") && p.Namespace.Contains(parentArea)
                                           )
                                       )
                                       ).ToList();

            if (selectedClass == null || selectedClass.Count == 0)
            {
                throw new Exception("SimpleSiteMapController: Parent node not found.");
            }

            if (selectedClass.Count > 1)
            {
                throw new Exception("SimpleSiteMapController: A Child node cannot have more than one Parent.");
            }

            var selectedMethodList = selectedClass.First().GetMethods().Where(d => d.Name.ToLower() == action.ToLower()).ToList();

            if (selectedMethodList == null || selectedMethodList.Count == 0)
            {
                throw new Exception("SimpleSiteMapController: Child method not defined or found.");
            }

            List <object> attributes = new List <object>();

            foreach (var selectedMethodItem in selectedMethodList)
            {
                attributes.AddRange(selectedMethodItem.GetCustomAttributes(false));
            }

            var attribute = attributes.Where(d => d.GetType() == typeof(SimpleSiteMapControllerAttribute)).ToList();

            if (attribute == null)
            {
                throw new Exception("SimpleSiteMapController: Attributes not found.");
            }

            if (attribute.Count > 1)
            {
                throw new Exception("SimpleSiteMapController: An ActionResult method cannot have more than one SimpleSiteMapControllerAttribute");
            }

            var attributeSelected = (SimpleSiteMapControllerAttribute)attribute.First();

            simpleSiteMapController.Label = attributeSelected.Label;

            simpleSiteMapController.SimpleSiteMapControllerParent = PopulateParent(filterContext, attributeSelected.ParentController, attributeSelected.ParentArea, attributeSelected.ParentAction);

            return(simpleSiteMapController);
        }