/// <summary> /// Formats and rewrites the link pattern with the specified <paramref name="linkName" />. /// </summary> /// <param name="linkName">Name of the link.</param> /// <param name="parameters">The parameters.</param> /// <param name="hashString">The hash string.</param> /// <param name="qualify">If set to <c>true</c> the resulting URL will be prefixed with <see cref="ServerPrefix"/>.</param> /// <returns>The full rewritten Url, ready to use</returns> /// <exception cref="ConfigurationError">Current configuration doesn't contain a link with name specified with /// <paramref name="linkName" />.</exception> internal string FormatAndRewriteUrl(string linkName, NameValueCollection parameters, string hashString = null, bool qualify = false) { if (!this.Links.ContainsKey(linkName)) { log.ErrorFormat("The url configuration doesn't contain a url with name '{0}'", linkName); return string.Format("javascript:alert('Unresolved link: {0}')", linkName); } string linkPattern = this.Links[linkName].Value; QueryString formatValues = new QueryString { { "locale", context.Locale }, { "category", context.Category } }; if (parameters != null) formatValues.Merge(parameters); string resultUrl = this.FormatPattern(linkPattern, formatValues); formatValues.Remove("locale"); formatValues.Remove("category"); string queryString = formatValues.ToString(); if (queryString != string.Empty) { string join = resultUrl.Contains("?") ? "&" : "?"; resultUrl = string.Concat(resultUrl, join, queryString); } if (!string.IsNullOrWhiteSpace(hashString)) { string join = resultUrl.Contains("#") ? string.Empty : "#"; resultUrl = string.Concat(resultUrl, join, hashString); } if (qualify) resultUrl = string.Concat(this.ServerPrefix, resultUrl); return resultUrl; }
internal static string GetSelfFunction(SageContext context, params string[] arguments) { var linkArguments = new LinkArguments(arguments, false, "encode", "absolute"); var currentUrl = linkArguments.Switches["absolute"] ? context.Url.RawUrl : context.Url.RawPathAndQuery; var paramQuery = new QueryString(parameterSeparators); if (currentUrl.Contains("?")) { var questionIndex = currentUrl.IndexOf("?", StringComparison.Ordinal); paramQuery.Parse(currentUrl.Substring(questionIndex + 1)); currentUrl = currentUrl.Substring(0, questionIndex); } paramQuery.Merge(linkArguments.QueryString); var result = string.Concat(currentUrl, paramQuery.ToString("?"), string.IsNullOrEmpty(linkArguments.HashString) ? string.Empty : string.Concat("#", linkArguments.HashString)); if (linkArguments.Switches["encode"] && !string.IsNullOrEmpty(result)) result = HttpUtility.UrlEncode(result); return result; }