Lowercase all URLs for actions and page transitions

Page creation date :

environment

Visual Studio
  • Visual Studio 2019
ASP.NET Core
  • 3.1 (MVC, Razor page)

Default URL case

ASP.NET to another URL, such as a link, in the Core Web app, Basically, it's a mixture of case.

The law is that for Razor pages, the .cshtml file name in the Pages folder, For MVC, the controller class name and action method name are used as is.

This naming law applies only ASP.NET core features to generate URLs. For example, those that specify asp-xxxxx for:

<!-- Razor ページの場合 -->
<a asp-area="" asp-page="/Privacy">Privacy</a>

<!-- MVC の場合 -->
<a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>

This does not include direct URL specifications as follows:

<a href="/Privacy">Privacy</a>

By the way, the URL generated follows the above law. ASP.NET Core is not case-sensitive for accessed URLs. It is accessible no how you put case in the URL.

Make all URLs lowercase

Access itself is case-insensitive, so you don't have to care about the problem as it works. Some people may not want the web browser to display capital letters because they display a URL.

In this case, .cs add the following code to startup and the link URL will be lowercase.

// 追加
using Microsoft.AspNetCore.Routing;

public class Startup
{
  public void ConfigureServices(IServiceCollection services)
  {
    services.AddRazorPages();

    // 追加
    services.Configure<RouteOptions>(options =>
    {
      // URL を小文字にする
      options.LowercaseUrls = true;
    });
  }
}