Slugify Service

One way to generate a valid URL, using data already obtained, is to make a slug. This service was in an earlier version named SuggestionService. But sometimes the service returned values that already was in use so it is replaced by the SlugifyService.

When creating a slug, the SlugifyService is used for converting text to an url. Eg. the name of the "about us" page will get the url-segment "about-us". Another common area of use, is slugifying the title on a blog to a url.

  • Slugify Service
    /// <summary>
    /// Slugify service will create a slug of the input text.
    /// </summary>
    [Service(ServiceType = typeof(SlugifyService), Lifetime = DependencyLifetime.Singleton)]
    public abstract class SlugifyService
        /// <summary>
        /// Make the slug from the specified text.
        /// </summary>
        /// <param name="cultureInfo">Culture info that should be used for the slug.</param>
        /// <param name="text">Input text that should be used for the slug.</param>
        /// <returns>Returns the slugified version of the text.</returns>
        public abstract string Slugify([NotNull] CultureInfo cultureInfo, string text);

The main method Slugify will create a slug of the given input text. CutureInfo is added to the method as a place holder for future use, current implementation does not make any difference between cultures yet.

  • SlugifyServiceImpl

These rule checks are executed in the default implementation of SlugifyService:

  1. Invalid Characters from UrlValidatorConfig.DefaultInvalidCharacters (static property of UrlValidatorConfig) and a running instance of urlValidatorconfig.InvalidCharacters (extendable) will be replace with "-".
  2. [åääâáàÅÄ] is replaced with "a".
  3. [öóòôöÖôóòØ] is replaced with "o"
  4. [éèêëÉËÊÈ€] is replaced with "e"
  5. [Ææ] is replaced with "ae"
  6. [üûúùÜÛÚÙ] is replaced with "u"
  7. "--" is replaced with "-"
  8. "__" is replaced with "_"
  9. If input string has extension match with UrlValidatiorConfig.DefaultInvalidExtensions or instance urlValidatorConfig.InvalidExtensions then that extension will be truncated.
  • Example:
Input Output
"test with space" "test-with-space"
"a.b,c;d:e+[f]g{s}w-" "a-b-c-d-e-f-g-s-w"
"a--b" "a-b"
"a__b" "a_b"
"a/\\\.,;: +*'§½""<>\|!?$&=@#%\{\}\^\~\[\]`´¨¤—–‘’“”b" "a-b"
"\u2007 \u00A0 \u0020 \u202F \u2060 \uFEFF" ""