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

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…


This content originally appeared on DEV Community and was authored by 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();

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();

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?


This content originally appeared on DEV Community and was authored by DEV Community


Print Share Comment Cite Upload Translate Updates
APA

DEV Community | Sciencx (2022-02-23T19:19:19+00:00) Build APIs for the Web INSTANTLY with ASP.NET Core 6. Retrieved from https://www.scien.cx/2022/02/23/build-apis-for-the-web-instantly-with-asp-net-core-6/

MLA
" » Build APIs for the Web INSTANTLY with ASP.NET Core 6." DEV Community | Sciencx - Wednesday February 23, 2022, https://www.scien.cx/2022/02/23/build-apis-for-the-web-instantly-with-asp-net-core-6/
HARVARD
DEV Community | Sciencx Wednesday February 23, 2022 » Build APIs for the Web INSTANTLY with ASP.NET Core 6., viewed ,<https://www.scien.cx/2022/02/23/build-apis-for-the-web-instantly-with-asp-net-core-6/>
VANCOUVER
DEV Community | Sciencx - » Build APIs for the Web INSTANTLY with ASP.NET Core 6. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/23/build-apis-for-the-web-instantly-with-asp-net-core-6/
CHICAGO
" » Build APIs for the Web INSTANTLY with ASP.NET Core 6." DEV Community | Sciencx - Accessed . https://www.scien.cx/2022/02/23/build-apis-for-the-web-instantly-with-asp-net-core-6/
IEEE
" » Build APIs for the Web INSTANTLY with ASP.NET Core 6." DEV Community | Sciencx [Online]. Available: https://www.scien.cx/2022/02/23/build-apis-for-the-web-instantly-with-asp-net-core-6/. [Accessed: ]
rf:citation
» Build APIs for the Web INSTANTLY with ASP.NET Core 6 | DEV Community | Sciencx | https://www.scien.cx/2022/02/23/build-apis-for-the-web-instantly-with-asp-net-core-6/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.