DEV Community

Dries Deboosere
Dries Deboosere

Posted on • Originally published at driesdeboosere.dev on

Lowercase URLs in ASP.NET Core.

When you create links in your ASP.NET Core web application with TagHelpers then each word in your URL will start with a capital letter.

Take this for example:

<a asp-controller="About" asp-action="Index">About</a>
<a asp-controller="Tools" asp-action="Index">Tools</a>
Enter fullscreen mode Exit fullscreen mode

This will result in following HTML code where each word from the controller and action name starts with a capital letter in the URL:

<a href="https://driesdeboosere.dev/About">About</a>
<a href="https://driesdeboosere.dev/Tools">Tools</a>
Enter fullscreen mode Exit fullscreen mode

And even in your web browsers address bar the generated URL from the controller and action name starts with a capital letter:

URL in address bar

I would like to have all my URLs in lowercase, like this:

<a href="https://driesdeboosere.dev/about">About</a>
<a href="https://driesdeboosere.dev/tools">Tools</a>
Enter fullscreen mode Exit fullscreen mode

Luckily we can easily set this up in our Startup.cs class in the ConfigureServices method. We just need to setup the route options by adding the AddRouting extension method to services and set the RouteOptions LowercaseUrls property to true.

services.AddRouting(options => options.LowercaseUrls = true);
Enter fullscreen mode Exit fullscreen mode

Be aware that you put this options BEFORE services.AddMvc() or services.AddControllersWithViews()!!

See this example:

public void ConfigureServices(IServiceCollection services)
{
    services.AddRouting(options => options.LowercaseUrls = true);
    services.AddControllersWithViews().AddRazorRuntimeCompilation();
    // other configurations
}
Enter fullscreen mode Exit fullscreen mode

Now all our URLs created by our TagHelpers will be in lowercase.

Top comments (0)