In ASP.NET Core 10, what does WebApplication.CreateSlimBuilder leave out compared with CreateBuilder, and how do you restore HTTPS or a regex route constraint?
answer
- UseKestrelCore, not UseKestrel
- AddRoutingCore drops the regex constraint
- no IIS integration, no Debug logger
- UseKestrelHttpsConfiguration brings TLS back
- SetParameterPolicy<RegexInlineRouteConstraint>("regex")
basics
~10 sCreateSlimBuilder keeps configuration, user secrets and console logging but drops Kestrel HTTPS and HTTP/3, IIS integration, extra logging providers, hosting startup and the regex route constraint. UseKestrelHttpsConfiguration restores HTTPS; AddRouting or SetParameterPolicy restores regex.
solid answer
~40 s`WebApplication.CreateSlimBuilder` is the builder the `webapiaot` template uses: it wires the minimum so the trimmer can drop the rest. It keeps `appsettings.json` and `appsettings.{Environment}.json`, user secrets in Development, environment variables, the command line, the `Logging` section and a simple console logger. It leaves out **HTTPS and HTTP/3** (it calls `UseKestrelCore`), **IIS integration**, the Debug, EventSource and EventLog providers, hosting startup assemblies, `UseStartup`, static web assets, the `{ApplicationName}.settings.json` files, and the **`regex` route constraint**, because it registers `AddRoutingCore` instead of `AddRouting`. You opt back in: `builder.WebHost.UseKestrelHttpsConfiguration()` for HTTPS, `UseQuic()` for HTTP/3, and `options.SetParameterPolicy<RegexInlineRouteConstraint>("regex")` (or `AddRouting()`) for regex routes. Without that, a `{name:regex(...)}` route throws `InvalidOperationException`.
code
csharp · 15 linesvar builder = WebApplication.CreateSlimBuilder(args);
// Kestrel terminates TLS itself in this deployment
builder.WebHost.UseKestrelHttpsConfiguration();
// the slim builder registers only a throwing stub for regex
builder.Services.Configure<RouteOptions>(options =>
options.SetParameterPolicy<RegexInlineRouteConstraint>("regex"));
var app = builder.Build();
app.MapGet("/images/{name:regex(^[a-z0-9-]+$)}", (string name) =>
TypedResults.Ok(name));
app.Run();go deeper
Remember that the AOT template uses CreateSlimBuilder, and that it serves plain HTTP only unless you opt in to HTTPS.
List what the slim builder drops: HTTPS and HTTP/3, IIS integration, extra logging providers, hosting startup and the regex constraint, and name the call that restores each one.
Treat each opt-in as a size cost, push TLS to the ingress where possible, and replace regex routes with handler validation in AOT services.
Set a house rule for which builder each service type starts from, so AOT services stay small and non-AOT services are not crippled by slim defaults.
## Why a slim builder exists Trimming removes code the app cannot reach. `WebApplication.CreateBuilder` reaches a lot: it configures HTTPS in Kestrel, IIS integration, several logging providers and the full routing setup, so all of that survives trimming whether you use it or not. **`WebApplication.CreateSlimBuilder`**, added in .NET 8 for Native AOT, wires only what a typical cloud API needs, so a Native AOT executable stays small. The **`webapiaot`** template calls it in `Program.cs`. (`CreateEmptyBuilder` goes further and wires almost nothing; it belongs with the other builders.) ## What it keeps and what it drops | Area | `CreateBuilder` | `CreateSlimBuilder` | |---|---|---| | `appsettings.json`, `appsettings.{Env}.json`, env vars, command line | yes | yes | | User secrets in Development | yes | yes | | `{ApplicationName}.settings.json` files | yes | **no** | | Logging providers | Console, Debug, EventSource, EventLog (Windows) | **simple console only** | | Kestrel | `UseKestrel`: HTTP, HTTPS, HTTP/3 | **`UseKestrelCore`: HTTP only** | | IIS integration | yes | **no** | | Hosting startup assemblies, `UseStartup` | yes | **no** | | Static web assets in Development | yes | **no** | | Routing services | `AddRouting` | **`AddRoutingCore`** (no `regex` constraint) | Both builders still register host filtering and the forwarded-headers startup filter, and both bind the `Kestrel` configuration section. ## Restoring HTTPS and HTTP/3 The template assumes TLS ends at an ingress or load balancer, so the container serves plain HTTP. If the process itself must terminate TLS, opt in explicitly: ```csharp var builder = WebApplication.CreateSlimBuilder(args); builder.WebHost.UseKestrelHttpsConfiguration(); // HTTPS endpoints and certificates builder.WebHost.UseQuic(); // HTTP/3, only if you need it ``` Calling `app.UseHttpsRedirection()` does not do this: the redirection middleware only sends clients to an HTTPS URL; it does not make Kestrel listen on one. ## Restoring the regex route constraint The other built-in constraints (`int`, `guid`, `alpha`, `minlength`, `range` and so on) stay registered. `regex` is registered as a **stub** that throws, because a general regular-expression engine for arbitrary patterns adds size. With the slim builder, this route fails: ```csharp app.MapGet("/images/{name:regex(^[a-z0-9-]+$)}", (string name) => name); ``` The stub throws an `InvalidOperationException` whose message names the fix. Two equivalent ways to re-enable it: 1. `builder.Services.Configure<RouteOptions>(o => o.SetParameterPolicy<RegexInlineRouteConstraint>("regex"));` 2. `builder.Services.AddRouting();`, which adds the same setup on top of `AddRoutingCore`. Often the better answer is to drop the regex from the route and validate the value inside the handler. ## InvariantGlobalization: the template's other default The `webapiaot` project file also sets **`<InvariantGlobalization>true</InvariantGlobalization>`**. In globalization-invariant mode the app loads no culture data, which shrinks the deployment and removes the ICU dependency from the container. The SDK also sets `PredefinedCulturesOnly` to `true` in that mode, so: - `CultureInfo.InvariantCulture` works as usual; - `new CultureInfo("de-DE")` throws **`CultureNotFoundException`**; - formatting and comparison use invariant rules everywhere. An image-resize API that only returns numbers and file names does not care. An API that formats prices or dates per user locale must set `InvariantGlobalization` to `false` and accept the extra size. ## Mistakes that show up in review - **Expecting HTTPS in local runs.** The `webapiaot` launch profile is HTTP-only; adding an `https` URL to it does not help until Kestrel's HTTPS support is opted in. - **Adding back everything at once.** Calling `UseKestrelHttpsConfiguration`, `UseQuic`, `AddRouting` and extra logging providers by reflex rebuilds most of `CreateBuilder` and gives back the size you trimmed. - **Assuming the missing pieces fail loudly at build.** The slim builder compiles fine with a `regex` route; the stub only throws when routing builds that endpoint, so an integration test that starts the app is what catches it. - **Forgetting the `{ApplicationName}.settings.json` files.** A team that keeps per-app settings in those files must move them into `appsettings.json` or add the file source explicitly. ## How to choose - Start from `CreateSlimBuilder` for a Native AOT Minimal API and add features one call at a time. - Use `CreateBuilder` for anything that needs IIS hosting, in-process HTTPS by default, or the features the slim builder deliberately omits. - Treat each opt-in as a size decision: every call you add keeps more code alive through trimming.
- Why is the alpha constraint still available on the slim builder when regex is not?In the ASP.NET Core 10 source, `alpha` stays in the default constraint map because it uses one fixed regular expression with a small size cost. `regex` accepts any pattern at run time, which keeps a general regex engine alive, so `AddRoutingCore` registers a stub that throws until you call `SetParameterPolicy<RegexInlineRouteConstraint>("regex")` or `AddRouting()`.
- What happens if code in a webapiaot app calls new CultureInfo("fr-FR")?The template sets `InvariantGlobalization` to `true`, and the SDK then sets `PredefinedCulturesOnly` to `true`, so creating any culture other than the invariant one throws `CultureNotFoundException`. If the API must format per locale, turn `InvariantGlobalization` off and ship culture data, at the cost of a larger app.
- CreateSlimBuilder serves HTTPS by default, just like CreateBuilder.
- app.UseHttpsRedirection() is enough to make a slim-builder app listen on HTTPS.
- CreateSlimBuilder skips appsettings.json, so configuration must come from environment variables.
- Every built-in route constraint, regex included, works the same on the slim builder.
- InvariantGlobalization only affects string sorting, so CultureInfo("de-DE") still works.