DEV Community

Supraja Tangella
Supraja Tangella

Posted on

๐—จ๐—ป๐—น๐—ฒ๐—ฎ๐˜€๐—ต๐—ถ๐—ป๐—ด ๐˜๐—ต๐—ฒ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—ผ๐—ณ ๐—ฆ๐—ถ๐—บ๐—ฝ๐—น๐—ถ๐—ฐ๐—ถ๐˜๐˜†: ๐——๐—ถ๐˜ƒ๐—ถ๐—ป๐—ด ๐——๐—ฒ๐—ฒ๐—ฝ ๐—ถ๐—ป๐˜๐—ผ ๐—”๐—ฆ๐—ฃ.๐—ก๐—˜๐—ง ๐—–๐—ผ๐—ฟ๐—ฒ ๐— ๐—ถ๐—ป๐—ถ๐—บ๐—ฎ๐—น ๐—”๐—ฃ๐—œ๐˜€

Minimal APIs in ASP.NET Core continue to redefine how we build fast, lightweight web APIs. What started as a simple concept has quickly evolved, offering significant performance gains and a richer feature set without sacrificing their core simplicity. Let's explore some of the latest advancements that make them an even more compelling choice for modern development.

๐Ÿญ. ๐—ฃ๐—ฒ๐—ฟ๐—ณ๐—ผ๐—ฟ๐—บ๐—ฎ๐—ป๐—ฐ๐—ฒ ๐—•๐—ผ๐—ผ๐˜€๐˜๐˜€: ๐—™๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฎ๐—ป๐—ฑ ๐—Ÿ๐—ถ๐—ด๐—ต๐˜๐—ฒ๐—ฟ

Minimal APIs are engineered for speed and efficiency. Recent enhancements have drastically reduced memory allocations and accelerated exception handling, making your lightweight HTTP APIs even more performant. This means quicker responses and more efficient resource utilization, especially under load.

๐—–๐—ผ๐—ฑ๐—ฒ ๐—˜๐˜…๐—ฎ๐—บ๐—ฝ๐—น๐—ฒ (๐—œ๐—บ๐—ฝ๐—น๐—ถ๐—ฐ๐—ถ๐˜ ๐—ฃ๐—ฒ๐—ฟ๐—ณ๐—ผ๐—ฟ๐—บ๐—ฎ๐—ป๐—ฐ๐—ฒ):

The beauty here is that performance improvements are often under the hood. Your existing Minimal API code just runs faster!

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

(No change needed, the improvement is in the runtime.)

๐Ÿฎ. ๐—ฅ๐—ถ๐—ฐ๐—ต๐—ฒ๐—ฟ ๐—™๐—ฒ๐—ฎ๐˜๐˜‚๐—ฟ๐—ฒ๐˜€: ๐—ฆ๐—ถ๐—บ๐—ฝ๐—น๐—ฒ ๐—ฌ๐—ฒ๐˜ ๐—–๐—ฎ๐—ฝ๐—ฎ๐—ฏ๐—น๐—ฒ

While maintaining their "minimal" philosophy, these APIs are gaining powerful capabilities. You now have improved support for OpenAPI (Swagger/documentation), robust authentication and authorization options, and more refined error handling, allowing you to build comprehensive APIs with less boilerplate.

๐—–๐—ผ๐—ฑ๐—ฒ ๐—˜๐˜…๐—ฎ๐—บ๐—ฝ๐—น๐—ฒ (๐—ข๐—ฝ๐—ฒ๐—ป๐—”๐—ฃ๐—œ & ๐—”๐˜‚๐˜๐—ต๐—ฒ๐—ป๐˜๐—ถ๐—ฐ๐—ฎ๐˜๐—ถ๐—ผ๐—ป):

Defining a route with a tag for OpenAPI and requiring authorization is straightforward.

app.MapGet("/securedata", () => "Sensitive Info")
.RequireAuthorization()
.WithTags("Data"); // Used by OpenAPI for grouping

๐Ÿฏ. ๐—ง๐˜†๐—ฝ๐—ฒ๐—ฑ๐—ฅ๐—ฒ๐˜€๐˜‚๐—น๐˜๐˜€: ๐—–๐—น๐—ฒ๐—ฎ๐—ป ๐—ฎ๐—ป๐—ฑ ๐—–๐—ผ๐—ป๐—ฐ๐—ถ๐˜€๐—ฒ ๐—ฅ๐—ฒ๐˜€๐—ฝ๐—ผ๐—ป๐˜€๐—ฒ๐˜€

Say goodbye to manual status code setting! TypedResults offer a convenient and type-safe way to return HTTP responses with specific status codes. This makes your API endpoints more readable, maintainable, and less prone to errors.

๐—–๐—ผ๐—ฑ๐—ฒ ๐—˜๐˜…๐—ฎ๐—บ๐—ฝ๐—น๐—ฒ (๐—ง๐˜†๐—ฝ๐—ฒ๐—ฑ๐—ฅ๐—ฒ๐˜€๐˜‚๐—น๐˜๐˜€):

Returning a "Not Found" or "OK" response is clear and explicit.

app.MapGet("/item/{id}", (int id) =>
{
if (id == 0) return Results.NotFound("Item not found.");
return Results.Ok($"Found item {id}");
});

Minimal APIs are maturing into a powerful tool for building efficient and scalable web services.

๐—ช๐—ต๐—ฎ๐˜ ๐—ฎ๐—ฟ๐—ฒ ๐˜†๐—ผ๐˜‚๐—ฟ ๐˜๐—ต๐—ผ๐˜‚๐—ด๐—ต๐˜๐˜€ ๐—ผ๐—ป ๐˜๐—ต๐—ฒ ๐—ฒ๐˜ƒ๐—ผ๐—น๐˜‚๐˜๐—ถ๐—ผ๐—ป ๐—ผ๐—ณ ๐— ๐—ถ๐—ป๐—ถ๐—บ๐—ฎ๐—น ๐—”๐—ฃ๐—œ๐˜€? ๐—›๐—ผ๐˜„ ๐—ฎ๐—ฟ๐—ฒ ๐˜†๐—ผ๐˜‚ ๐—น๐—ฒ๐˜ƒ๐—ฒ๐—ฟ๐—ฎ๐—ด๐—ถ๐—ป๐—ด ๐˜๐—ต๐—ฒ๐—บ ๐—ถ๐—ป ๐˜†๐—ผ๐˜‚๐—ฟ ๐—ฝ๐—ฟ๐—ผ๐—ท๐—ฒ๐—ฐ๐˜๐˜€? ๐—ฆ๐—ต๐—ฎ๐—ฟ๐—ฒ ๐˜†๐—ผ๐˜‚๐—ฟ ๐—ฒ๐˜…๐—ฝ๐—ฒ๐—ฟ๐—ถ๐—ฒ๐—ป๐—ฐ๐—ฒ๐˜€ ๐—ฏ๐—ฒ๐—น๐—ผ๐˜„!

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.