How to force lowercase URLs in ASP.NET 5

Posted on Wednesday, November 18, 2015 at 2:23 AM into csharp, code & seo by Steve Woods.
Roughly a 1 minute read.

One of the most annoying things about ASP.NET development is that the Routing components default to being in CamelCase so you end up with URLs like /About/Index instead of /about/index.

In terms of SEO, a URL is classed as distinct based on its case, so the first and second URL are treated as two pages even though they share the same content - this can have a detrimental effect on your page ranking in Google because it classes one of the URLs as duplicate content.

Besides, it's always nice to have consistency in a web app and since the rest of the world seems to default to lowercase (and it looks better) t's a good idea to make it the default setting for your application.

You can of course use redirects in IIS to automatically do this, but that usually involves a 301 redirect which can be fairly expensive if you have a high-traffic website.

Here's a simple solution that works for all generated URLs in ASP.NET Routing, for instance those generated using Tag Helpers, i.e.:

@Html.ActionLink("Home", "Index", "Click Me")

will output:

<a href="/home/index">Click Me</a>

For ASP.NET 5

Add the following to startup.cs in the ConfigureServices() method:

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

For ASP.NET 4

Add the following to your RegisterRoutes() method in your RouteConfig Class

routes.LowercaseUrls = true;

I hope you find this useful!

;

Comments

Please consider what you post!
Chillax if you're angry.