skip to content

In ASP.NET Core 10, what do RouteOptions.LowercaseUrls, LowercaseQueryStrings and AppendTrailingSlash change, and what can LowercaseUrls break?

level: middleimportance: nice to knowfreq 20%

answer

  1. all three default to false
  2. generation only, not matching
  3. lowercases route values in the path too
  4. LinkOptions overrides per call
  5. case-sensitive tokens in the path

basics

~20 s

They only affect generated links: LowercaseUrls lowercases the whole generated path, LowercaseQueryStrings the query keys and values, AppendTrailingSlash adds a final slash. All default to false. LowercaseUrls also lowercases route values, so case-sensitive codes in the path get mangled.

solid answer

~40 s

`builder.Services.Configure<RouteOptions>(o => o.LowercaseUrls = true)` makes `LinkGenerator`, `Url.Action`, tag helpers and `CreatedAtAction` emit lowercase paths, so `BikesController.Details` links become `/bikes/details/7`. It does not change matching; literal matching is already case-insensitive, so mixed-case URLs keep working. All three options default to `false`. `LowercaseQueryStrings` lowercases query keys and values and is documented to work with `LowercaseUrls`. `AppendTrailingSlash` adds `/` to generated paths. The trap: the whole path is lowercased with `ToLowerInvariant`, route values included, so a case-sensitive booking code like `{code}` = `Bk7QxZ` comes out as `bk7qxz` and no longer finds the booking. Keep such values in the query string, make lookups case-insensitive, or override per call with `LinkOptions`.

code

csharp · 17 lines
csharp
var builder = WebApplication.CreateBuilder(args);
builder.Services.Configure<RouteOptions>(o => o.LowercaseUrls = true);
var app = builder.Build();

app.MapGet("/Bookings/ByCode/{code}", (string code) => Results.Ok(code))
   .WithName("BookingByCode");

app.MapGet("/links", (LinkGenerator links) => new
{
    global = links.GetPathByName("BookingByCode", new { code = "Bk7QxZ" }),
    // "/bookings/bycode/bk7qxz"  - the code is lowercased too
    perCall = links.GetPathByName("BookingByCode", new { code = "Bk7QxZ" },
        options: new LinkOptions { LowercaseUrls = false })
    // "/Bookings/ByCode/Bk7QxZ"
});

app.Run();

go deeper

for a junior

Recall that RouteOptions.LowercaseUrls makes generated links lowercase and is off by default.

for a middle

Explain that the options affect link generation only, list the three switches, and show how to set them with Configure<RouteOptions>.

for a senior

Spot that route values are lowercased too, protect case-sensitive identifiers with query parameters or LinkOptions, and combine with transformers for kebab-case.

for a principal

Set a canonical URL policy covering casing, trailing slashes and redirects, so generated links, crawlers and old bookmarks agree.

## The three switches `RouteOptions` carries three settings that shape **generated** URLs. In the v10.0.12 source all three are plain `bool` properties with no initializer, so they default to `false`: | Property | Effect on generated URLs | |---|---| | `LowercaseUrls` | the path, including route values, is lowercased | | `LowercaseQueryStrings` | query string keys and values are lowercased; documented to be used together with `LowercaseUrls` | | `AppendTrailingSlash` | a `/` is appended to the generated path if it does not end with one | You set them once: ```csharp builder.Services.Configure<RouteOptions>(options => { options.LowercaseUrls = true; options.LowercaseQueryStrings = false; options.AppendTrailingSlash = false; }); ``` Everything that generates links through endpoint routing picks them up: `LinkGenerator`, `IUrlHelper` (`Url.Action`, `Url.RouteUrl`, `Url.Page`), anchor tag helpers, redirect results and `CreatedAtAction` / `CreatedAtRoute`. ## Where to set them `Configure<RouteOptions>` can be called from anywhere in `Program.cs`, and `AddRouting(options => ...)` is an equivalent overload. MVC apps typically set `LowercaseUrls` once with the rest of the routing setup. Because the options feed `DefaultLinkGenerator`, which is a singleton built from `IOptions<RouteOptions>`, changing them at run time has no effect; they are startup configuration. To verify the effect, generate a link in a test with `LinkGenerator` from the test host's services and assert its exact text, including any route values that must keep their case. ## Generation, not matching These options do **not** change how requests are matched. Literal segments already match case-insensitively, so after turning on `LowercaseUrls` a bookmarked `/Bikes/Details/7` still reaches the same action. They also do not redirect mixed-case requests to the lowercase form; doing that needs URL rewriting. ## What LowercaseUrls actually lowercases In `UriBuildingContext`, when `LowercaseUrls` is on, every piece of text appended to the path, literal or parameter value, goes through `ToLowerInvariant()`. That has a consequence teams discover in production. In a bike-rental API: ```csharp app.MapGet("/bookings/by-code/{code}", (string code, BookingStore store) => store.FindByCode(code) is { } b ? Results.Ok(b) : Results.NotFound()) .WithName("BookingByCode"); ``` With `LowercaseUrls = true`, `GetPathByName("BookingByCode", new { code = "Bk7QxZ" })` returns `/bookings/by-code/bk7qxz`. If booking codes are case-sensitive, the e-mailed link now returns 404 from the handler. Options: 1. Make the code case-insensitive by design, such as lowercase-only alphabets. 2. Pass the code as a query parameter, which `LowercaseUrls` leaves alone unless `LowercaseQueryStrings` is also on. 3. Override for one call with `LinkOptions`, which has nullable `LowercaseUrls`, `LowercaseQueryStrings` and `AppendTrailingSlash` properties that win over the global options: `links.GetPathByName("BookingByCode", new { code }, options: new LinkOptions { LowercaseUrls = false })`. ## Relationship to parameter transformers `LowercaseUrls` is a blunt tool: lowercase everything. If you want `BookingHistory` to become `booking-history` rather than `bookinghistory`, that is a job for an outbound parameter transformer on the route tokens, which changes both matching and generation for controller and action names. The two can be combined. ## AppendTrailingSlash `AppendTrailingSlash` exists for sites whose canonical URLs end in `/`. Like the other two, it only changes generated links, not how requests are matched. If you enable it, make sure redirects and canonical tags agree, or crawlers see two versions of every page. ## What interviewers listen for - Defaults are `false`, and the options affect generation, not matching. - Route values are lowercased too, which can break case-sensitive identifiers. - `LinkOptions` gives per-call control. - Kebab-case is a transformer's job, not `LowercaseUrls`.

  • After enabling LowercaseUrls, do old mixed-case links such as /Bikes/Details/7 stop working?
    No. The option only changes URLs the app generates. Matching of literal segments is case-insensitive, so `/Bikes/Details/7` and `/bikes/details/7` reach the same endpoint. If you want one canonical form for search engines, add a redirect with URL rewriting; routing will not do it for you.
  • How do you turn LowercaseUrls off for a single generated link?
    Pass a `LinkOptions` to the `LinkGenerator` call: `new LinkOptions { LowercaseUrls = false }`. Its properties are nullable booleans; a non-null value overrides the global `RouteOptions` for that call only. This is useful for the few routes that carry case-sensitive values.

  • Believes LowercaseUrls is on by default in new ASP.NET Core projects.
  • Thinks LowercaseUrls makes routing reject mixed-case request URLs.
  • Assumes only literal segments are lowercased, not route values.
  • Uses LowercaseUrls to get kebab-case controller names like booking-history.
  • Expects LowercaseUrls to redirect old mixed-case URLs to lowercase ones.