Ejemplo n.º 1
0
        public TokenizedLink CreateLink(Func<string, object> substitution, bool removeIfNull = false)
        {
            var newLink = new TokenizedLink(Rel, Href);
            UpdateHref(newLink, substitution, removeIfNull);

            return newLink;
        }
Ejemplo n.º 2
0
        private static void UpdateHref(TokenizedLink link, Func<string, object> substitution, bool removeIfNull)
        {
            var subName = substitution.Method.GetParameters()[0].Name.Trim('_');
            var token = string.Format("{{{0}}}", subName);

            if (PositionInHref(token, link) <= -1) return;

            var val = substitution(null);
            var hasVal = val != null;

            if (hasVal)
            {
                link.Href = link.CreateUri(substitution).ToString();
                return;
            }

            if (!removeIfNull) return;

            if (!IsQuerystring(token, link))
                link.Href = link.Href.Replace(token + HrefSlash, string.Empty);
            else
            {
                //remove querystring pairs if remove is true
                if (string.IsNullOrWhiteSpace(link._hrefQueryString))
                    return;

                var queryVals =
                    link._hrefQueryString.Split(new[] {HrefQueryStringStart, HrefQueryStringAppend},
                                                StringSplitOptions.RemoveEmptyEntries).ToList();
                queryVals.RemoveAll(v => v.Contains(token));

                var newQueryVals = string.Join(HrefQueryStringAppend, queryVals);

                link.Href = link.Href.Replace(link._hrefQueryString, string.Empty);
                if (!string.IsNullOrWhiteSpace(newQueryVals))
                    link.Href = link.Href + HrefQueryStringStart + newQueryVals;
            }
        }
Ejemplo n.º 3
0
 private static int PositionInHref(string searchFor, TokenizedLink link)
 {
     var origHref = link._origHref;
     return PositionIn(searchFor, origHref);
 }
Ejemplo n.º 4
0
 private static bool IsQuerystring(string token, TokenizedLink link)
 {
     var hrefQueryStringStartPosition = link._hrefQueryStringStartPosition;
     if (hrefQueryStringStartPosition < 0) return false;
     return PositionInHref(token, link) > hrefQueryStringStartPosition;
 }
Ejemplo n.º 5
0
 public TokenizedLink TemplateWithoutQuerystring()
 {
     var newLink = new TokenizedLink(Rel, _origHref.Replace(_hrefQueryString, string.Empty));
     return newLink;
 }