Organizing Parameters in Minimal API with the AsParametersAttribute

Even though it was introduced in .NET 7, I came across recently the AsParametersAttribute. Let's have a look what it is good for.

Organinzing paramters

Let's have a look at the following Minimal API:

app.MapGet("/test/{id:int}/{name}", (int id, string name, [FromQuery(Name="foo")] string bar) => id);

We have three parameters scattered around the method signature. It is not a big deal for this simple example, but it can get messy with more parameters (and services). So we can use the AsParameters attribute to group them together:

// Use the attribute in front of your entity
app.MapGet("/test/{id:int}/{name}", ([AsParameters]TestRequestDto dto) => dto.Id);

// Could also be a regular class, struct or record struct
public record TestRequestDto(int Id, string Name, [FromQuery(Name="foo")] string Bar);

Now everything is encapsulated in the TestRequestDto and the method signature is much cleaner. A small drawback is that there can be a very small overhead in performance (like measurable not necessarily noticable) and allocations (if you take a class).

How many API's does .NET have?

.NET is big, very big! So how many API's does it have? Let's find out!

Comparing the performance between the Minimal API and classic Controllers

Today, we are going to benchmark the performance of the Minimal API in ASP.NET 9 (and for reference against ASP.NET 8 as well) against the classic Controllers. We will test a few scenarios and check how the performance of the Minimal API compares to the classic Controllers. Let's get right into it!

Building a Minimal ASP.NET Core clone

In this article, we will build a minimal version of what ASP.NET Core does - yes, you read right. We will create a very simplistic clone of ASP.NET Core to discuss how the whole thing works. Beginning with a simple console application, we will add the necessary components to make it work as a web server. Bonus points for our own middleware pipeline and dependency injection.

An error has occurred. This application may no longer respond until reloaded. Reload x