Пример #1
0
        private static MvcHtmlString BuildRouteLink(this HtmlHelper htmlHelper, string linkText, string routeName, object routeValues = null, object htmlAttributes = null, string surroundUsing = "li", object surrounderAttributes = null, bool detectAndMarkAsActive = true, string activeClassName = "active", MvcHtmlString innerHtml = null)
        {
            var isCurrentRequest = false;

            if (detectAndMarkAsActive)
            {
                isCurrentRequest = IsCurrentRequest(new UrlHelper(htmlHelper.ViewContext.RequestContext), routeName);
            }

            var htmlAttributesDictionary = RouteValueDictionaryExtensions.Convert(htmlAttributes);

            if (isCurrentRequest && string.IsNullOrEmpty(surroundUsing))
            {
                if (htmlAttributesDictionary.ContainsKey("class"))
                {
                    htmlAttributesDictionary["class"] += string.Concat(" ", activeClassName);
                }
                else
                {
                    htmlAttributesDictionary.Add("class", activeClassName);
                }
            }

            var link = htmlHelper.RouteLink(linkText, routeName, RouteValueDictionaryExtensions.Convert(routeValues), htmlAttributesDictionary);

            if (string.IsNullOrEmpty(surroundUsing))
            {
                return(link);
            }

            var tag = new TagBuilder(surroundUsing);

            tag.MergeAttributes(RouteValueDictionaryExtensions.Convert(surrounderAttributes));

            if (isCurrentRequest)
            {
                tag.AppendAttribute("class", activeClassName);
            }

            tag.InnerHtml = link.ToHtmlString();

            if (innerHtml != null)
            {
                tag.InnerHtml += innerHtml;
            }

            return(new MvcHtmlString(tag.ToString(TagRenderMode.Normal)));
        }