public static NavigationRouteBuilder MapNavigationRoute <T>(this RouteCollection routes, string displayName, Expression <Func <T, ActionResult> > action) where T : IController
        {
            var newRoute = new NamedRoute("", "", new MvcRouteHandler());

            newRoute.ToDefaultAction(action);
            newRoute.DisplayName = displayName;
            routes.Add(newRoute.Name, newRoute);
            return(new NavigationRouteBuilder(routes, newRoute));
        }
        public static void MapNavigationRoute(this RouteCollection routes, string name, string url, object defaults,
                                              object constraints = null)
        {
            var newRoute = new NamedRoute(name, url, new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(defaults), Constraints = new RouteValueDictionary(constraints)
            };

            routes.Add(name, newRoute);
        }
        public static NavigationRouteBuilder AddChildRoute <T>(this NavigationRouteBuilder builder, string DisplayText, Expression <Func <T, ActionResult> > action) where T : IController
        {
            var childRoute = new NamedRoute("", "", new MvcRouteHandler());

            childRoute.ToDefaultAction <T>(action);
            childRoute.DisplayName = DisplayText;
            childRoute.IsChild     = true;
            builder._parent.Children.Add(childRoute);
            builder._routes.Add(childRoute.Name, childRoute);
            return(builder);
        }
        public static NamedRoute ToDefaultAction <T>(this NamedRoute route, Expression <Func <T, ActionResult> > action) where T : IController
        {
            var body = action.Body as MethodCallExpression;

            if (body == null)
            {
                throw new ArgumentException("Expression must be a method call");
            }

            if (body.Object != action.Parameters[0])
            {
                throw new ArgumentException("Method call must target lambda argument");
            }

            string actionName = body.Method.Name;

            // check for ActionName attribute
            var attributes = body.Method.GetCustomAttributes(typeof(ActionNameAttribute), false);

            if (attributes.Length > 0)
            {
                var actionNameAttr = (ActionNameAttribute)attributes[0];
                actionName = actionNameAttr.Name;
            }

            string controllerName = typeof(T).Name;

            if (controllerName.EndsWith("Controller", StringComparison.OrdinalIgnoreCase))
            {
                controllerName = controllerName.Remove(controllerName.Length - 10, 10);
            }

            ;
            route.Defaults = LinkBuilder.BuildParameterValuesFromExpression(body) ?? new RouteValueDictionary();
            foreach (var pair in route.Defaults.Where(x => x.Value == null).ToList())
            {
                route.Defaults.Remove(pair.Key);
            }

            route.Defaults.Add("controller", controllerName);
            route.Defaults.Add("action", actionName);

            route.Url  = CreateUrl(actionName, controllerName);
            route.Name = "Navigation-" + controllerName + "-" + actionName;

            if (route.DataTokens == null)
            {
                route.DataTokens = new RouteValueDictionary();
            }
            route.DataTokens.Add("Namespaces", new string[] { typeof(T).Namespace });

            return(route);
        }
        public static void MapNavigationRoute(this RouteCollection routes, string name, string displayName, string url,
                                              object defaults,
                                              string[] namespaces,
                                              object constraints = null)
        {
            var newRoute = new NamedRoute(name, displayName, url, new MvcRouteHandler())
            {
                Defaults    = new RouteValueDictionary(defaults),
                Constraints = new RouteValueDictionary(constraints),
                DataTokens  = new RouteValueDictionary()
            };

            if (namespaces != null && namespaces.Length > 0)
            {
                newRoute.DataTokens["Namespaces"] = namespaces;
            }

            routes.Add(name, newRoute);
        }
Exemplo n.º 6
0
        public static MvcHtmlString NavigationListItemRouteLink(this HtmlHelper html, NamedRoute route)
        {
            var li = new TagBuilder("li")
            {
                InnerHtml = html.RouteLink(route.DisplayName, route.Name).ToString()
            };

            if (CurrentRouteMatchesName(html, route.Name))
            {
                li.AddCssClass("active");
            }
            if (route.Children.Count() > 0)
            {
                //TODO: create a UL of child routes here.
                li.AddCssClass("dropdown");
                li.InnerHtml = "<a href=\"#\" class=\"dropdown-toggle\" data-toggle=\"dropdown\">" + route.DisplayName + "<b class=\"caret\"></b></a>";
                var ul = new TagBuilder("ul");
                ul.AddCssClass("dropdown-menu");

                foreach (var child in route.Children)
                {
                    var childLi = new TagBuilder("li");
                    childLi.InnerHtml = html.RouteLink(child.DisplayName, child.Name).ToString();
                    ul.InnerHtml     += childLi.ToString();
                }
                //that would mean we need to make some quick

                li.InnerHtml = "<a href='#' class='dropdown-toggle' data-toggle='dropdown'>" + route.DisplayName + " <b class='caret'></b></a>" + ul.ToString();
            }
            return(MvcHtmlString.Create(li.ToString(TagRenderMode.Normal)));
        }
 public NavigationRouteBuilder(RouteCollection routes, NamedRoute parent)
 {
     _routes = routes;
     _parent = parent;
 }