I write about minimal Web APIs in 2016 and my goal has always been for “dotnet server.cs” to allow for a single file simple Web API. Fast forward to 2021 and there’s some great work happening again in the minimal API space! Let’s do a ’ We updated .NET SDK templates to use the latest C# language features and patterns. We hadn’t revisited the templates in terms of new language features in a while. It was time to do that and we’ll ensure that the templates use new and modern features going forward. The following language features are used in the new templates:
This is pretty cool. Perhaps initially a bit of a shock, but this a major version and a lot of work is being done to make C# and .NET more welcoming. All your favorite things are still there and will still work but we want to explore what can be done in this new space. Richard puts the reasoning very well:
This means you’ll see new things when you make something totally new from scratch but your existing stuff will mostly work just fine. I haven’t had any trouble with my sites. Let’s look at a super basic hello world that returns text/plain: var builder = WebApplication.CreateBuilder(args); Slick. Note that I made line 3 (which is optional) just be one line to be terse. Not needed, just trying on these new shoes. If we make this do more and support MVC, it’s just a little larger. I could add in var builder = WebApplication.CreateBuilder(args); Back to the original Web API one. I can add Open API support by adding a reference to var builder = WebApplication.CreateBuilder(args); Then I hit https://localhost:5001/swagger and I get the SwaggerUI and a little WebAPI Tester: Anuraj has a great blog where he goes deeper and pokes around David Fowlers GitHub and creates a minimal WebAPI with Entity Framework and an in-memory database with full OpenAPI support. He put the source at at https://github.com/anuraj/MinimalApi so check that out. Bipin Joshi did a post also earlier in June and explored in a bit more detail how to hook up to real data and noted how easy it was to return entities with JSON output as the default. For example: app.UseEndpoints(endpoints => { That’s it! Very clean. Dave Brock did a tour as well and did Hello World in just three lines, but of course, you’ll note he used var app = WebApplication.Create(args); Dave does point out how nice it is to work with simple models using the C# record keyword which removes a LOT of boilerplate cruft. Check this out! var app = WebApplication.Create(args); That’s it, and if you hit /person you’ll get back a nice JSON WebAPI with this result: { Dig even deeper by checking out Maria Naggaga’s presentation in June that’s on YouTube where she talks about the thinking and research behind Minimal APIs and shows off more complex apps. Maria also did another great talk in the same vein for the Microsoft Reactor so check that out as well. Is this just about number of lines of code? Have we moved your cheese? Will these scale to production? This is about enabling the creation of APIs that encapsulate best practices but can give you the “middleware-like” performance with the clarity and flexibility that was previous available with all the ceremony of MVC. Here’s some more resources:
Have fun! Lots of cool things happening this year, even in the middle of the panini. Stay safe, friends. Sponsor: Pluralsight helps teams build better tech skills through expert-led, hands-on practice and clear development paths. For a limited time, get 50% off your first month and start building stronger skills. © 2021 Scott Hanselman. All rights reserved. Exploring a minimal Web API with ASP.NET Core 6 published first on http://7elementswd.tumblr.com/ via Tumblr Exploring a minimal Web API with ASP.NET Core 6
0 Comments
Leave a Reply. |
About UsIf you want to keep up with the latest in web design, you have to be willing to study and learn on an ongoing basis. If you are a web developer or web designer, you’ll find a lot of useful tutorials and code snippets collection here. Furthermore, we also discuss more general topics of web design and how the role of a web designer has evolved over the years.
|