Make URL Slugs In ASP.NET Using C#

I'll be showing you how to write a universal algorithm for producing URL friendly slugs in any C# based solution incuding ASP.Net as well as ASP.Net Core.

Introduction 

 
Let us start up by loosening up and indulging in an inside joke...
 
Joke 1
 
"Why do Java Developers wear glasses?
    Because they can't see-sharp."
 
I could not resist but to manufacture a relevant comeback for the above.
 
Joke 2
 
"Why do C# Developers love coffee?
    Because Java is best served in a cup."
 
Alright, enough with the humor. Let us get right into it.
 
Below is a universal algorithm that produces slugs by following the steps below.
  • Removing all accents (e.g s => s etc...);
  • Making the slug lower case;
  • Removing all special characters;
  • Removing all additional spaces in favor of just one;
  • and Replacing all the spaces with a hyphen"-"
Here's the solution below,
  1. using System.Globalization;  
  2. using System.Linq;  
  3. using System.Text;  
  4. using System.Text.RegularExpressions;  
  5.   
  6. namespace ProjectTitle.Extensions  
  7. {  
  8.     /// <summary>  
  9.     /// Contains all custom written string related Extension Methods.  
  10.     /// </summary>  
  11.     public static class StringExtensions  
  12.     {  
  13.         /// <summary>  
  14.         /// Removes all accents from the input string.  
  15.         /// </summary>  
  16.         /// <param name="text">The input string.</param>  
  17.         /// <returns></returns>  
  18.         public static string RemoveAccents(this string text)  
  19.         {  
  20.             if (string.IsNullOrWhiteSpace(text))  
  21.                 return text;  
  22.   
  23.             text = text.Normalize(NormalizationForm.FormD);  
  24.             char[] chars = text  
  25.                 .Where(c => CharUnicodeInfo.GetUnicodeCategory(c)   
  26.                 != UnicodeCategory.NonSpacingMark).ToArray();  
  27.   
  28.             return new string(chars).Normalize(NormalizationForm.FormC);  
  29.         }  
  30.           
  31.         /// <summary>  
  32.         /// Turn a string into a slug by removing all accents,   
  33.         /// special characters, additional spaces, substituting   
  34.         /// spaces with hyphens & making it lower-case.  
  35.         /// </summary>  
  36.         /// <param name="phrase">The string to turn into a slug.</param>  
  37.         /// <returns></returns>  
  38.         public static string Slugify(this string phrase)  
  39.         {  
  40.             // Remove all accents and make the string lower case.  
  41.             string output = phrase.RemoveAccents().ToLower();  
  42.   
  43.             // Remove all special characters from the string.  
  44.             output = Regex.Replace(output, @"[^A-Za-z0-9\s-]""");  
  45.   
  46.             // Remove all additional spaces in favour of just one.  
  47.             output = Regex.Replace(output, @"\s+"" ").Trim();  
  48.   
  49.             // Replace all spaces with the hyphen.  
  50.             output = Regex.Replace(output, @"\s""-");  
  51.   
  52.             // Return the slug.  
  53.             return output;  
  54.         }  
  55.     }  
  56. }

Conclusion

 
There you have it, a Slugify algorithm that takes care of whatever end-users will be throwing at it.
 
Please provide feedback below in the comments, critique wherever possible and I hope this solution helps everyone in the long run.