C# Foundations·Practice drills 5

Drill 12: Recall — A minimal ASP.NET API

Close the lesson and reproduce the example from memory.

Beginner 5 min read Lesson 130 of 300 Challenge included
What you will be able to do
  • Close the lesson and reproduce the example from memory.

Practice task

Close the lesson and reproduce the example from memory.

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

app.MapGet("/api/courses", async (AppDb db) =>
    Results.Ok(await db.Courses.ToListAsync()));

app.MapPost("/api/courses", async (CourseInput input, AppDb db) =>
{
    if (string.IsNullOrWhiteSpace(input.Title))
        return Results.BadRequest(new { error = "INVALID_TITLE" });

    var course = new Course(Guid.NewGuid(), input.Title, input.Lessons);
    db.Courses.Add(course);
    await db.SaveChangesAsync();
    return Results.Created("/api/courses/" + course.Id, course);
});

app.Run();
Your turn

Write your answer

Easy

Close the lesson and reproduce the example from memory.

TEXT · starter
Your answer…
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Lesson 130 of 300 · 0% of course