Routing

Routing is how ASP.NET Core decides which piece of your code handles a given request — matching an HTTP method and a URL pattern to a handler.

Mapping routes

app.MapGet, app.MapPost, app.MapPut, and app.MapDelete each register a handler for one HTTP method and route pattern:

C# Program.cs
app.MapGet("/", () => "Welcome to the API");
app.MapGet("/status", () => "OK");
app.MapPost("/items", () => "Item created");
Terminal — curl requests
$ curl http://localhost:5225/status
OK
$ curl -X POST http://localhost:5225/items
Item created

A request only matches if both the HTTP method and the path match exactly — a GET /items request would not hit the MapPost("/items", ...) handler above at all; it would fall through with a 404.

Route parameters

Curly braces in a route pattern capture part of the URL and pass it straight into the handler as a typed parameter:

C# Program.cs
app.MapGet("/hello/{name}", (string name) => $"Hello, {name}!");
app.MapGet("/products/{id:int}", (int id) => $"Product #{id}");
Terminal — curl requests
$ curl http://localhost:5225/hello/Priya
Hello, Priya!
$ curl http://localhost:5225/products/42
Product #42
$ curl http://localhost:5225/products/abc
404 Not Found

{id:int} adds a route constraint — ASP.NET only matches this route if the segment can actually parse as an int, which is why /products/abc falls through to a 404 instead of reaching the handler with a type error.

Query strings

Values after a ? in the URL don't need a route placeholder — bind them by matching a parameter name:

C# Program.cs
app.MapGet("/search", (string q) => $"Searching for: {q}");
Terminal
$ curl "http://localhost:5225/search?q=laptops"
Searching for: laptops
Note: route order can matter when patterns could both match the same URL — ASP.NET's routing is generally smart about picking the more specific match, but two ambiguous routes registered for the exact same pattern and method will throw an exception at startup rather than silently picking one.