/// <summary> /// Converts a title, phrase etc. to friendly URL format. /// </summary> /// <param name="data">The data to convert</param> /// <returns>SEO friendly string</returns> public static string ConvertForController(this string data) { //ensure we have all lower case stuff var strData = data.ConvertToAscii().ToLower(); strData = Regex.Replace(strData, @"-", " "); //Strip illegal bytes strData = Regex.Replace(strData, @"[^a-z0-9\s-]", ""); //Reduce multiple spaces to single strData = Regex.Replace(strData, @"\s+", " ").Trim(); //Replace space with empty string strData = Regex.Replace(strData, @"\s", ""); //Return friendly URL return strData; }
/// <summary> /// Converts a title, phrase etc. to friendly URL format. /// </summary> /// <param name="data">The data to convert</param> /// <returns>SEO friendly string</returns> public static string ConvertForSeo(this string data) { //ensure we have all lower case stuff var strData = data.ConvertToAscii().ToLower(); strData = Regex.Replace(strData, @"-", " "); //Replace space with hyphen for SEO strData = Regex.Replace(strData, @"\\", "-"); //Replace space with hyphen for SEO strData = Regex.Replace(strData, @"\/", "-"); //Replace any dots with hyphen for SEO strData = Regex.Replace(strData, @"\.+", "-"); //Strip illegal bytes strData = Regex.Replace(strData, @"[^a-z0-9\s-]", ""); //Reduce multiple spaces to single strData = Regex.Replace(strData, @"\s+", " ").Trim(); //Replace space with hyphen for SEO strData = Regex.Replace(strData, @"\s", "-"); if (strData.Length > 70) { //Limit seo title to 70 chars strData = strData.Truncate(70); //remove any partially completed words if (strData.Contains("-")) { strData = strData.Substring(0, strData.LastIndexOf("-", StringComparison.Ordinal)); } } //Return friendly URL return strData; }