Variables and route parameters

What a variable is; personalising responses with parameters.

1. What is this?

A variable stores a value under a name: string name = "Ava". A route parameter is a placeholder in a URL: /hello/{name} captures whatever comes after /hello/ into name. So visiting /hello/Liam replies Hello, Liam.

2. Why do we use it?

Profiles, products, orders — all are the same page shape with different values. Parameters make one route serve millions of pages.

3. Syntax breakdown

  • string name = "Ava";

    Variable holding text.

  • app.MapGet("/hello/{name}", (string name) => $"Hello, {name}");

    {name} captures that URL segment; the handler receives it as parameter name.

  • Console.WriteLine($"Hello, {name}");

    Runnable version: interpolation substitutes the variable.

5. Common mistakes

  • Mismatching placeholder and parameter names ({name} vs (string user)).
  • Forgetting $ before interpolated strings.
  • Assuming the URL value has the right type — validate it.

Practice task

Add string city = "Berlin"; and print "Hello, Ava from Berlin." with interpolation.

Hint

$"Hello, {name} from {city}."

Try it below — tests run automatically when you press Run.

dotnet

Output

Press Run to see output.

Mini quiz

3 questions. Answer, then check.

  1. 1. What is a route parameter?

  2. 2. What does $"...{x}..." do?

  3. 3. Why validate route values?

4. Example — edit and run

dotnet

Output

Press Run to see output.

Expected output: Hello, Ava