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!

Marking API's as obsolete or as experimental

Often times your API in your program or library evolves. So you will need a mechanism of telling that a specific API (an interface or just a simple method call) is obsolete and might be not there anymore in the next major version.

Also it can happen that you have a preview version of a API, which might not be rock-stable and the API-surface might change. How do we indicate that to the user?

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