Build APIs for the Web INSTANTLY with ASP.NET Core 6

Jeffrey T. Fritz - Feb 23 '22 - - Dev Community

I've been building web-based applications for many years, and I always run into the same questions and code that needs to be written again and again. How do we get data into and out of the database for 90% of our interactions? The simple create, read, update, and delete operations... we've simplified those processes over time with ORM tools like NHibernate, Entity Framework, and Dapper. The next question is, how do we get that data into our applications easily? We want to create a repeatable process that is easy to follow and quick to integrate.

Typically, folks have been building some sort of API that a web server responds with. Great, now we have to write ANOTHER layer of code over the database and generate records for all of those things we just exposed to our application with an ORM.

Enter Minimal APIs

In ASP.NET Core 6, we can dramatically simplify the creation of these APIs for interaction with the database by using the new Minimal API approach. Our API code can be delivered in 1 file, referencing the Entity Framework context that connects to the database.

Consider this configuration to expose a Contacts table as a web endpoint that can be accessed with an HTTP GET verb:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddSqlite<MyContext>(
  "Data Source=contacts.db"
);

var app = builder.Build();

app.MapGet("/api/Contacts", async (MyContext db) => 
  await db.Contacts.ToArrayAsync()
);
app.MapGet("/api/Contacts/{id:int}", async (MyContext db, int id) => {
    var contact = db.Contacts.FirstOrDefaultAsync(x => x.Id == id);
    if (contact == null) return Results.NotFound();
    return Results.Ok(contact);
});

app.Run();
Enter fullscreen mode Exit fullscreen mode

That's still a LOT of boilerplate code to just generate the GET and GET by id actions. We still have 3 more methods to write, and that's just for this first entity.

Generate the code... do it in 1 line

This got me thinking, and I wanted to make this easier to build with sensible defaults. I put together a simple prototype that would generate ALL 5 of the default HTTP actions with a NuGet package and 1 line of code.

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddSqlite<MyContext>(
  "Data Source=contacts.db"
);

var app = builder.Build();

app.MapInstantAPIs<MyContext>();

app.Run();
Enter fullscreen mode Exit fullscreen mode

Now we're talking! This one statement generates the following HTTP endpoints:

  • GET /api/Contact
  • GET /api/Contact/{id}
  • POST /api/Contact
  • PUT /api/Contact/{id}
  • DELETE /api/Contact/{id}

I put together a demo discussing this library and showing how it generates the same code that you might write by hand:

This is still just a prototype, but if you would like to learn more, check out our GitHub repository where we are welcoming discussions and pull requests. You can even try out the library yourself by downloading the latest NuGet package.

How are you building APIs for the web? How would you like to see it made easier and more repeatable?

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .