新建处理类:

public class SlugifyParameterTransformer : IOutboundParameterTransformer
{
    public string? TransformOutbound(object? value)
    {
        if(value == null) return null;
        string? result = value.ToString();
        if(result == null) return null;

        return Regex.Replace(result, "([a-z])([A-Z])", "$1-$2").ToLower();
    }
}

路由注入 Program.cs :

builder.Services.AddRouting(options => {
    options.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer);
    options.LowercaseUrls = true;
});

路由:

app.MapControllerRoute(name: "default", pattern: "{controller:slugify=Home}/{action:slugify=Index}/{id?}");

标签: none

添加新评论