Ejemplo n.º 1
0
        //public static string CurrentAction(this UrlHelper urlHelper) {
        //    var routeValueDictionary = urlHelper.RequestContext.RouteData.Values;

        //    // In case using virtual dirctory
        //    var rootUrl = urlHelper.Content("~/");
        //    return string.Format("{0}{1}/{2}/", rootUrl, routeValueDictionary["controller"], routeValueDictionary["action"]);
        //}

        public static string CurrentActionPath(this ViewContext viewContext)
        {
            var rootUrl  = viewContext.RootUrl();
            var areaName = viewContext.AreaName();

            if (!String.IsNullOrEmpty(areaName))
            {
                areaName += "/";
            }

            var controllerName = viewContext.ControllerName();
            var actionName     = viewContext.ActionName();

            return(string.Format("{0}{1}{2}/{3}/",
                                 rootUrl,
                                 areaName,
                                 controllerName,
                                 actionName));
        }
Ejemplo n.º 2
0
    /// <summary>
    /// Process TagHelper
    /// </summary>
    /// <param name="context">TagHelperContext</param>
    /// <param name="output">TagHelperOutput</param>
    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        // Return if no menu or items to output
        if (Menu is null || Menu.Items.Count == 0)
        {
            output.SuppressOutput();
            return;
        }

        // Setup objects
        var currentController = ViewContext.ControllerName().ToLower(CultureInfo.InvariantCulture);
        var currentAction     = ViewContext.ActionName().ToLower(CultureInfo.InvariantCulture);
        var urlHelper         = UrlHelperFactory.GetUrlHelper(ViewContext);

        // Build the menu
        BuildMenu(
            Menu.Items,
            mi => mi.Controller.ToLower(CultureInfo.InvariantCulture) == currentController,
            mi => mi.Controller,
            el => output.Content.AppendHtml(el)
            );

        // Set output properties
        output.TagName = WrapperElement;
        output.TagMode = TagMode.StartTagAndEndTag;
        output.Attributes.Add("class", WrapperClass);

        // Build a menu (allows recursion for child menus)
        //
        //		items:		The menu items to build a menu for
        //		isActive:	Function which determines whether or not the current item is the active page
        //		getText:	If MenuItem.Text is null, returns the text to show in the link
        //		append:		Action to append the element to the menu wrapper
        void BuildMenu(List <MenuItem> items, Func <MenuItem, bool> isActive, Func <MenuItem, string> getText, Action <TagBuilder> append)
        {
            // Add each menu item to the menu
            foreach (var menuItem in items)
            {
                // Create item element
                var item = new TagBuilder(ItemElement);
                item.AddCssClass(ItemClass);

                // Create link element
                var link = new TagBuilder("a");
                link.AddCssClass(LinkClass);

                // Add GUID
                link.MergeAttribute("id", $"link-{menuItem.Id}");

                // Check whether or not this is an active link / section
                if (isActive(menuItem))
                {
                    link.AddCssClass(LinkActiveClass);
                }

                // Generate the link URL
                var urlActionContext = new UrlActionContext
                {
                    Controller = menuItem.Controller,
                    Action     = menuItem.Action,
                    Values     = menuItem.RouteValues
                };

                link.Attributes.Add("href", urlHelper.Action(urlActionContext));

                // Add link text - if not set use getText() function
                _ = link.InnerHtml.Append(menuItem.Text ?? getText(menuItem));

                // Add the link to the list item
                _ = item.InnerHtml.AppendHtml(link);

                // Check for child menu
                if (IncludeChildren && menuItem.Children.Count > 0)
                {
                    // Create child menu wrapper
                    var childMenu = new TagBuilder(WrapperElement);
                    childMenu.AddCssClass(ChildMenuWrapperClass);

                    // Build child menu
                    BuildMenu(
                        menuItem.Children,
                        mi => mi.Action.ToLower(CultureInfo.InvariantCulture) == currentAction,
                        mi => mi.Action,
                        el => childMenu.InnerHtml.AppendHtml(el)
                        );

                    // Add child menu to item
                    _ = item.InnerHtml.AppendHtml(childMenu);
                }

                // Append item
                append(item);
            }
        }
    }