예제 #1
0
        /// <summary>
        /// Cleans up a string to make a legal url part
        /// </summary>
        /// <param name="urlName">The URL part text.</param>
        /// <param name="punctuationReplacement">The text with which to replace invalid characters</param>
        /// <param name="maxLength">The maximum length of the URL part</param>
        /// <returns>The cleaned URL part name</returns>
        private static string MakeUrlSafe(string urlName, string punctuationReplacement = "-", int maxLength = 50)
        {
            const string IllegalCharacters  = "#%&*{}\\:<>?/+'.";
            const string UnwantedCharacters = ";,\"+!'{}[]()^$*";

            if (urlName == null)
            {
                urlName = string.Empty;
            }

            urlName = urlName.Normalize(NormalizationForm.FormD);
            var outUrl = new StringBuilder(urlName.Length);

            var i = 0;

            foreach (char c in urlName)
            {
                if (!IllegalCharacters.Contains(c.ToString()))
                {
                    // can't have leading .. or trailing .
                    if (!((i <= 0 || i == urlName.Length) && c == '.'))
                    {
                        if (c == ' ' || UnwantedCharacters.Contains(c.ToString()))
                        {
                            // replace spaces, commas and semicolons
                            outUrl.Append(punctuationReplacement);
                        }
                        else if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
                        {
                            outUrl.Append(c);
                        }
                    }
                }

                i++;
                if (i >= maxLength)
                {
                    break;
                }
            }

            string result = outUrl.ToString();

            // replace double replacements
            string doubleReplacement = punctuationReplacement + punctuationReplacement;

            if (result.Contains(doubleReplacement))
            {
                result = result.Replace(doubleReplacement, punctuationReplacement);

                // once more for triples
                result = result.Replace(doubleReplacement, punctuationReplacement);
            }

            // Global.asax Application_BeginRequest checks for these values and will try to redirect to the non-existent page
            if (result.EndsWith("install", StringComparison.CurrentCultureIgnoreCase) ||
                result.EndsWith("installwizard", StringComparison.CurrentCultureIgnoreCase))
            {
                result = result.Substring(0, result.Length - 1);
            }

            return(result);
        }
 /// <summary>
 /// Replaces unsafe symbols with dashes
 /// </summary>
 /// <param name="input">String to sanitize</param>
 /// <returns>Sanitized string</returns>
 public static string Sanitize(string input)
 => IllegalCharacters.Aggregate(input, (current, bad) => current.Replace(bad, '-'));