50

ASP.NET Core uses CamelCase-Routes like http://localhost:5000/DashboardSettings/Index by default. But I want to use lowercase routes, which are delimitted by dashes: http://localhost:5000/dashboard-settings/index They're more common and consistent, cause my application extends a website running Wordpress, which also has lowercase urls with dashes.

I learned that I can change the urls to lowercase using the routing-options:

services.ConfigureRouting(setupAction => {
    setupAction.LowercaseUrls = true;
});

This works but gave me urls without any delimiter like http://localhost:5000/dashboardsettings/index which are badly readable. I could define custom routes using the route attribute like

[Route("dashboard-settings")]
class DashboardSettings:Controller {
    public IActionResult Index() {
        // ...
    }
}

But that causes extra-work and is error-prone. I would prefer an automatic solution which search for uppercase chars, insert a dash before them and make the uppercase-char lowercase. For the old ASP.NET this was not a big issue, but on ASP.NET Core I see no direction how to handle this.

Whats the way to do this here? I need some kind of interface where I can generate urls (like for the tag helpers) and replace there the CamelCase by dash-delimiters. Then I need another kind of interface for the routing, so that the dash-delimiter urls are converted back to CamelCase for correct matching with my controller/action names.

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
Lion
  • 16,606
  • 23
  • 86
  • 148

6 Answers6

55

Update in ASP.NET Core Version >= 2.2

To do so, first create the SlugifyParameterTransformer class should be as follows:

public class SlugifyParameterTransformer : IOutboundParameterTransformer
{
    public string TransformOutbound(object value)
    {
        // Slugify value
        return value == null ? null : Regex.Replace(value.ToString(), "([a-z])([A-Z])", "$1-$2").ToLower();
    }
}

For ASP.NET Core 2.2 MVC:

In the ConfigureServices method of the Startup class:

services.AddRouting(option =>
{
    option.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer);
});

And route configuration should be as follows:

app.UseMvc(routes =>
{
    routes.MapRoute(
       name: "default",
       template: "{controller:slugify}/{action:slugify}/{id?}",
       defaults: new { controller = "Home", action = "Index" });
 });

For ASP.NET Core 2.2 Web API:

In the ConfigureServices method of the Startup class:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc(options => 
    {
        options.Conventions.Add(new RouteTokenTransformerConvention(new SlugifyParameterTransformer()));
    }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

For ASP.NET Core >=3.0 MVC:

In the ConfigureServices method of the Startup class:

services.AddRouting(option =>
{
    option.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer);
});

and route configuration should be as follows:

app.UseEndpoints(endpoints =>
{
    endpoints.MapAreaControllerRoute(
        name: "AdminAreaRoute",
        areaName: "Admin",
        pattern: "admin/{controller:slugify=Dashboard}/{action:slugify=Index}/{id:slugify?}");

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

For ASP.NET Core >=3.0 Web API:

In the ConfigureServices method of the Startup class:

services.AddControllers(options => 
{
    options.Conventions.Add(new RouteTokenTransformerConvention(new SlugifyParameterTransformer()));
});

For ASP.NET Core >=3.0 Razor Pages:

In the ConfigureServices method of the Startup class:

services.AddRazorPages(options => 
{
    options.Conventions.Add(new PageRouteTransformerConvention(new SlugifyParameterTransformer()));
});

This is will make /Employee/EmployeeDetails/1 route to /employee/employee-details/1

TanvirArjel
  • 30,049
  • 14
  • 78
  • 114
  • **Duplicate answer** of https://stackoverflow.com/questions/36358751/how-do-you-enforce-lowercase-routing-in-asp-net-core – Jack Oct 03 '20 at 22:49
  • 1
    Bazillion likes to you! – eestein Apr 23 '21 at 11:07
  • Doesn't work for `[Route("MyController/MyAction")]`. `MyController` becomes `my-controller`, but `MyAction` stays unchanged. – maxc137 Aug 25 '21 at 12:18
  • @МаксимКошевой You have added the pure string in your route so you have to write it by yourself. You can write: `[Route("my-controller/my-action")]` or `[Route("[controller]/[action]")]` – TanvirArjel Aug 26 '21 at 05:31
  • Oh, so this solution only affects tokens, got it. Whole URL is affected by `LowercaseUrls` though. Would be great if there would be something for slugify – maxc137 Aug 26 '21 at 06:20
  • Why does the `TransformOutbound` method receives an `object` parameter instead of `string`? – Marc.2377 Sep 20 '21 at 22:56
  • When using System.Text.RegularExpressions to process untrusted input, pass a timeout. A malicious user can provide input to RegularExpressions causing a Denial-of-Service attack. ASP.NET Core framework APIs that use RegularExpressions pass a timeout. – Frederik Krautwald Aug 18 '23 at 23:12
15

A little late to the party here but.. Can do this by implementing IControllerModelConvention.

 public class DashedRoutingConvention : IControllerModelConvention
 {
        public void Apply(ControllerModel controller)
        {
            var hasRouteAttributes = controller.Selectors.Any(selector =>
                                               selector.AttributeRouteModel != null);
            if (hasRouteAttributes)
            {
                // This controller manually defined some routes, so treat this 
                // as an override and not apply the convention here.
                return;
            }

            foreach (var controllerAction in controller.Actions)
            {
                foreach (var selector in controllerAction.Selectors.Where(x => x.AttributeRouteModel == null))
                {
                    var template = new StringBuilder();

                    if (controllerAction.Controller.ControllerName != "Home")
                    {
                        template.Append(PascalToKebabCase(controller.ControllerName));
                    }

                    if (controllerAction.ActionName != "Index")
                    {
                        template.Append("/" + PascalToKebabCase(controllerAction.ActionName));
                    }

                    selector.AttributeRouteModel = new AttributeRouteModel()
                    {
                        Template = template.ToString()
                    };
                }
            }
        }

        public static string PascalToKebabCase(string value)
        {
            if (string.IsNullOrEmpty(value))
                return value;

            return Regex.Replace(
                value,
                "(?<!^)([A-Z][a-z]|(?<=[a-z])[A-Z])",
                "-$1",
                RegexOptions.Compiled)
                .Trim()
                .ToLower();
        }
}

Then registering it in Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc(options => options.Conventions.Add(new DashedRoutingConvention()));
}

Can find more info and example here https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing

spottedmahn
  • 14,823
  • 13
  • 108
  • 178
Oliver
  • 1,490
  • 18
  • 19
  • It works, but if I have /Home/UrlWithDash action method and UrlWithDashController, throws an Exception (ambiguous reference). So, be careful while naming controllers and actions. – Silvio Delgado Oct 22 '19 at 16:59
12

I'm using Asp.NetCore 2.0.0 and Razor Pages (no explicit controller necessary), so all that's needed is:

  1. Enable Lowercase Urls:

    services.AddRouting(options => options.LowercaseUrls = true);

  2. Create a file named Dashboard-Settings.cshtml and the resulting route becomes /dashboard-settings

KTCO
  • 2,115
  • 23
  • 21
7

Copied from ASP.NET Core 3.0 (unchanged from 2.2) documentation:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc(options =>
    {
        options.Conventions.Add(new RouteTokenTransformerConvention(
                                     new SlugifyParameterTransformer()));
    });
}

public class SlugifyParameterTransformer : IOutboundParameterTransformer
{
    public string TransformOutbound(object value)
    {
        if (value == null) { return null; }

        // Slugify value
        return Regex.Replace(value.ToString(), "([a-z])([A-Z])", "$1-$2").ToLower();
    }
}
Long Mai
  • 177
  • 2
  • 10
0

Thanks for the information, however it's better to filter the selectors, in order to skip those with a custom route template : [HttpGet("/[controller]/{id}")] for example)

foreach (var selector in controllerAction.Selectors
                                         .Where(x => x.AttributeRouteModel == null))
Adrien Constant
  • 301
  • 3
  • 3
0

See Docs for the latest way to do it. Here is how you would do it for ASP.NET Core 7.0:

// IMPORTS
using System.Text.RegularExpressions;

namespace YourApi;

public class Program {
    public static void Main(string[] args) {
        // replace builder.Services.AddControllers() with the following
        builder.Services.AddControllersWithViews(options => {
            options.Conventions.Add(new RouteTokenTransformerConvention(
                                         new SlugifyParameterTransformer()));
        });
        // EXISTING CODE
    }
}

// https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing#use-a-parameter-transformer-to-customize-token-replacement
public class SlugifyParameterTransformer : IOutboundParameterTransformer {
    public string? TransformOutbound(object? value) {
        if (value == null) { return null; }
         return Regex.Replace(value.ToString(),
                             "([a-z])([A-Z])",
                             "$1-$2",
                             RegexOptions.CultureInvariant,
                                 TimeSpan.FromMilliseconds(100)).ToLowerInvariant();
    }
}
Elijah
  • 1,814
  • 21
  • 27