Benny Sutton is 4 hire - just click here

Extension Method GenerateSlug

Convert a string into hyphen separated words (for SEO friendly names) removing invalid characters e.g. from the string "look's \ Like tHis" it returns "looks-like-this"

             
        public static string GenerateSlug(this string phrase)
        {
            string str = phrase.RemoveAccent().ToLower();

            str = Regex.Replace(str, @"[^a-z0-9\s-]", ""); // invalid chars          
            str = Regex.Replace(str, @"\s+", " ").Trim(); // convert multiple spaces into one space  
            str = Regex.Replace(str, @"\s", "-"); // hyphens  

            return str;
        }
        
    

It also requires this to remove accents e.g. ^

             
               public static string RemoveAccent(this string txt)
        {
            byte[] bytes = System.Text.Encoding.GetEncoding("Cyrillic").GetBytes(txt);
            return System.Text.Encoding.ASCII.GetString(bytes);
        }
        
    

Extension methods are easy to chain, call like this... CategoryName.GenerateSlug(); NOT like this GenerateSlug(CategoryName)