ASP.NET Core · Module 1 · Lesson 03 / 04

Understand URLs and routes

Make two different URLs respond differently.

Routes connect URLs to actions

A URL path such as / or /about tells the server which resource was requested. ASP.NET Core routing matches paths to endpoints.

Write two endpoints

Replace Program.cs with:

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () => "Welcome to my first site!");
app.MapGet("/about", () => "This is my about page.");

app.Run();

Run the app and visit both / and /about using the local base address displayed by the terminal.

Add a route parameter

Place this line before app.Run():

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

Visit /hello/Asha. The word Asha is passed into the name parameter.

Practice

  1. Create a route /contact that returns a message.
  2. Create /square/{number:int} that returns number * number.
  3. Try an unknown path. What response status do you get?