ASP.NET uses PascalCase URL components by default, but I'd like to use kebab-case instead. I have seen various articles and SO answers achieve this elegantly in ASP.NET Core via RouteTokenTransformerConvention (example code below). I like this strategy because it 1) ensures kebab-case routes are mapped to the correct controller and action with the correct route parameters without repetitive manual work and 2) allows you to generate matching kebab-case links.
However, it does not touch URL queries. Is there a way to get the above benefits but also include query keys in the transformation? For example:
- I would want a URL like:
some-route?product-category=some+product+category... - To set the
ProductCategoryproperty of the bound model to"some product category"- (without me having to decorate
ProductCategorywith[FromQuery(Name="product-category")]or use this slightly better idea or use some other manual solution)
- (without me having to decorate
- The query value should not be transformed - just the key.
Here's my code taken from the above links as a starting point:
Hyphenator.cs
using Microsoft.AspNetCore.Routing;
using System.Text.RegularExpressions;
public class Hyphenator: IOutboundParameterTransformer
{
public string TransformOutbound(object value)
{
if (value == null)
{
return null;
}
return Regex.Replace(value.ToString(), "([a-z])([A-Z])", "$1-$2").ToLower();
}
}
Startup.cs
services.AddControllersWithViews(options =>
{
options.Conventions.Add(new RouteTokenTransformerConvention(new Hyphenator()));
});
Edit for clarification:
I am hoping for a solution that does the transformation in the outbound direction (like the example code above) so that aside from model binding working, I could also pass regular PascalCase property names to code that generates URLs (e.g. anchor tag helpers and methods like RedirectToRoute()) and have the query keys in those URLs end up being kebab-case in the user's browser. Is this possible?