Advertisement
Intermediate ASP.NET Core, Endpoint Metadata, Route Constraints & Templates

How do route parameter transformers let you customize the casing convention of generated URLs, such as converting PascalCase action names to kebab-case URLs?

Short answer

IOutboundParameterTransformer implementations (registered via RouteOptions) can transform route values during URL generation, most commonly used to automatically convert PascalCase controller/action names into kebab-case URL segments (a common REST API convention), avoiding the…

Save ASP.NET Core mock interview
Advertisement
AModel answer
IOutboundParameterTransformer implementations (registered via RouteOptions) can transform route values during URL generation, most commonly used to automatically convert PascalCase controller/action names into kebab-case URL segments (a common REST API convention), avoiding the need to manually specify a custom [Route] template on every single action just for casing.
public class KebabCaseParameterTransformer : IOutboundParameterTransformer {
    public string? TransformOutbound(object? value) =>
        value is null ? null : Regex.Replace(value.ToString()!, "([a-z])([A-Z])", "$1-$2").ToLower();
}

builder.Services.Configure<RouteOptions>(options => {
    options.ConstraintMap["kebab"] = typeof(KebabCaseParameterTransformer);
});
// Controller action 'GetProductDetails' automatically routes as /get-product-details
Real-world example
An API standardizing on kebab-case URLs (a common REST convention) applies a single global parameter transformer instead of manually annotating every controller and action with an explicit lowercase-hyphenated [Route] template.
Likely follow-ups
  • What other casing conventions might a transformer implement (snake_case, etc.)?
  • How does this interact with minimal APIs, which don't use PascalCase action names the same way?
Related topics