Skip to content

Instantly share code, notes, and snippets.

@marcominerva
Last active November 15, 2023 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcominerva/504ca0947b966ee4fefd1fae0ef567d6 to your computer and use it in GitHub Desktop.
Save marcominerva/504ca0947b966ee4fefd1fae0ef567d6 to your computer and use it in GitHub Desktop.
Slugify routes
builder.Services.AddControllers(options =>
{
options.Conventions.Add(new RouteTokenTransformerConvention(new SlugifyParameterTransformer()));
});
public class SlugifyParameterTransformer : IOutboundParameterTransformer
{
public string? TransformOutbound(object? value)
=> value is null ? null
: Regex.Replace(value.ToString()!, "([a-z])([A-Z])", "$1-$2", RegexOptions.CultureInvariant).ToLowerInvariant();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment