Last active
November 15, 2023 15:25
-
-
Save marcominerva/504ca0947b966ee4fefd1fae0ef567d6 to your computer and use it in GitHub Desktop.
Slugify routes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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