C# Foundations·Practice drills 2

Drill 3: Real project — A minimal ASP.NET API

Connect this example to a real project you could build.

Beginner 5 min read Lesson 40 of 300 Challenge included
What you will be able to do
  • Connect this example to a real project you could build.

Practice task

Connect this example to a real project you could build.

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

Connect this example to a real project you could build.

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 40 of 300 · 0% of course