Getting started with creating ASP.NET Web API services

In Visual Studio (2022 in this example)| select File > New > Project.

Select to create a new ASP.NET Core Web API Application

Give your project a name

Uncheck the use of controllers, to produce a minimalist application

Modify the default code inside Program.cs that is created

Program.cs

using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<TodoDb>(opt => opt.UseInMemoryDatabase("TodoList"));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
var app = builder.Build();

app.MapGet("/", () => "Hello World!");

app.MapGet("/todoitems", async (TodoDb db) =>
    await db.Todos.ToListAsync());

app.MapGet("/todoitems/complete", async (TodoDb db) =>
    await db.Todos.Where(t => t.IsComplete).ToListAsync());

app.MapGet("/todoitems/{id}", async (int id, TodoDb db) =>
    await db.Todos.FindAsync(id)
        is Todo todo
            ? Results.Ok(todo)
            : Results.NotFound());

app.MapPost("/todoitems", async (Todo todo, TodoDb db) =>
{
    db.Todos.Add(todo);
    await db.SaveChangesAsync();

    return Results.Created($"/todoitems/{todo.Id}", todo);
});

app.MapPut("/todoitems/{id}", async (int id, Todo inputTodo, TodoDb db) =>
{
    var todo = await db.Todos.FindAsync(id);

    if (todo is null) return Results.NotFound();

    todo.Name = inputTodo.Name;
    todo.IsComplete = inputTodo.IsComplete;

    await db.SaveChangesAsync();

    return Results.NoContent();
});

app.MapDelete("/todoitems/{id}", async (int id, TodoDb db) =>
{
    if (await db.Todos.FindAsync(id) is Todo todo)
    {
        db.Todos.Remove(todo);
        await db.SaveChangesAsync();
        return Results.Ok(todo);
    }

    return Results.NotFound();
});

app.Run();

class Todo
{
    public int Id { get; set; }
    public string? Name { get; set; }
    public bool IsComplete { get; set; }
}

class TodoDb : DbContext
{
    public TodoDb(DbContextOptions<TodoDb> options)
        : base(options) { }

    public DbSet<Todo> Todos => Set<Todo>();
}

Add the NuGet packages needed to support the database and diagnostics used in this example.

Select Tools menu > NuGet Package Manager > Manage NuGet Packages for Solution.

Make sure “Package source” is set to “All”.

Find Microsoft.EntityFrameworkCore.InMemory using the search box, select the Project checkbox in the right pane and then select Install.

Repeat for the Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore package.

Re-build and run the project.

Notice that on running the project the default “Hello World” API is run on the browser:

We can test the various GET/POST commands using an application like Postman. Be sure to copy over the correct port number.

Notice the first use of the API to return all “todoitems” returns 200 OK response, albeit with an empty array since the database has yet to be populated.

Now use the POST command to create a new “todoitem”:

So that when we call the API to return all “todoitems” again observe the list is no longer empty:

See the following post for instructions on how to call the Web API service using a C# console application:

https://www.technical-recipes.com/2018/how-to-consume-a-web-api-service-from-a-console-application

`