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
- Create a route
/contactthat returns a message. - Create
/square/{number:int}that returnsnumber * number. - Try an unknown path. What response status do you get?