Start learning free

LESSON 13 OF 173 · BACKEND: YOUR FIRST API

Your First Route

Practice

A web server is a program that waits for requests and answers them. A browser, or any other program, sends a request: a method, like GET ("give me"), and a path, like / or /products. The server sends back a response: a status code that says how it went, and a body.

In ASP.NET Core, the line var builder = WebApplication.CreateBuilder(); starts the setup, and builder.Build() makes your app. A route teaches the app to answer a path. app.MapGet takes the path, then the function that answers it:

app.MapGet("/hello", () => "Hi there!");

When a GET request for /hello arrives, ASP.NET Core calls the function and sends back whatever it returns, with the status 200, which means OK. A path that no route matches gets 404 Not Found, with an empty body.

Your app must answer:

GET /          →  200  Welcome to the shop!
GET /products  →  404  (empty body)

Your task: the builder is already created for you. Build the app and keep it in a variable named app, then map GET / to a function that returns the text "Welcome to the shop!".


Assignment
Follow the Coding Tutorial to build the app and write the shop's first route, GET /.


Hint
builder.Build() makes the app. In app.MapGet, the path is a string, then a comma, then the function () => .... End each statement with a semicolon.


STEP 1 OF 2
    Ctrl + Enter to check