Slug Class

Provides methods for generating URL-friendly slugs from text.

var slug = Slug.Create("This is a test!");
Assert.Equal("This-is-a-test", slug);

var lowerSlug = Slug.Create("Hello World", new SlugOptions { CasingTransformation = CasingTransformation.ToLowerCase });
Assert.Equal("hello-world", lowerSlug);
public static class Slug

Methods

NameDescription
Create(string?)Creates a slug from the specified text using default options.
Create(string?, SlugOptions?)Creates a slug from the specified text using the specified options.

Create overloads

Create(string?)

Slug.cs

Creates a slug from the specified text using default options.

public static string? Create(string? text)
Parameters
text string?
The text to convert to a slug.
Returns

string? A slug generated from the input text, or null if text is null.

Remarks

Text that is not well-formed Unicode is accepted: each ill-formed character is treated as Rune.ReplacementChar. In invariant globalization mode, where the normalizer accepts ill-formed text, such characters are left as they are.

The result is an empty string when no character of text is allowed, which is the case for any text written outside the default SlugOptions.AllowedRanges - every Cyrillic, Greek, Arabic or CJK title, for instance. Callers that put the slug in a URL segment or a file path should handle that before using it.

Create(string?, SlugOptions?)

Slug.cs

Creates a slug from the specified text using the specified options.

public static string? Create(string? text, SlugOptions? options)
Parameters
text string?
The text to convert to a slug.
options SlugOptions?
The options to use for slug generation, or null to use default options.
Returns

string? A slug generated from the input text, or null if text is null.

Remarks

Text that is not well-formed Unicode is accepted: each ill-formed character is treated as Rune.ReplacementChar. In invariant globalization mode, where the normalizer accepts ill-formed text, such characters are left as they are.

The result is an empty string when no character of text is allowed, which is the case for any text written outside the default SlugOptions.AllowedRanges - every Cyrillic, Greek, Arabic or CJK title, for instance. Callers that put the slug in a URL segment or a file path should handle that before using it.