Post Page Advertisement [Top]

    Beginning in November 2019, the basic API's began. The community began comparing the number of files and lines of code required to accomplish almost the same in C# and other languages, and it quickly became clear that C# is more complex than the others.

    And that wind come to .Net world finally ASP.NET Core 6 introduced a very simple hosting model that can be allow us to implement lightweight APIs with with few dependencies. This is significantly reduce lines of code that you want to develop. I've prepared my demo in 10 minutes.

    It seems .NET Core team wanted to reduce the complexity for all developers easily say welcome to (newcomers and veterans) and improve minimalism which is nice.

    As in an classical .Net API, you can use Authorization and Authentication, Logging, Validations (i.e FluentValidatio), Dependenciy Injections, Mapping.

On the otherhand we still have some limitations: 

  • No support for filters: i.e ExceptionFilter, IAsyncResultFilter
  • No support for model binding: i.e. IModelBinderProvider, IModelBinder
  • No support for ApiVersioning.

    Let's start, you can create minimal APIs while installing .Net 6 SDK or if you already have VS 2022 you already have a .Net 6 environment.

    Let's create a new Web API project and uncheck the User Contorllers area to be able to use minimal APIs.

    We are trying to create a simple API project with common CRUD operations. And we are planning to use EntityFrameworkCore InMemoryDatabase. Please install Microsoft.EntityFrameworkCore and Microsoft.EntityFrameworkCore.InMemory nuget packages.

Let's create our model Variant.cs

        public class Variant


            public int Id { get; set; }

            public string? Name { get; set; }

            public string? Type { get; set; }

            public DateTime CreatedAt { get; set; }


and create A VariantRequest.cs class without Id and CreatedAt properties. If we want to take a look our DB context class it should look like bellow: 

        public class ApiContext : DbContext


            public DbSet<Variant> Variants { get; set; }

            public ApiContext(DbContextOptions<ApiContext> options)

                : base(options){ }


Implementation of the CRUD Methods in the Minimal APIs

  app.MapPost("/Variants", async(VariantRequest Variant, ApiContext context) =>


    var createdVariant = context.Variants.Add(new Variant


        Name = Variant.Name ?? string.Empty,

        Type = Variant.Type ?? string.Empty,

        CreatedAt = DateTime.Now,


        await context.SaveChangesAsync();

return Results.Created($"/Variants/{createdVariant.Entity.Id}", createdVariant.Entity);


 We include the route pattern as the first parameter for all methods. ApiContext is resolved in the delegate of the initial MapGet implementation because it has been registered as a resolvable type. We add Id as a second parameter in the delegate of the second MapGet implementation for Get from Id or Delete form Id methods.

Be organized

    You can put your methods all to Program.cs but The Program class can grow and can be large and contain many lines of code while you developing your minimal APIs. Let's demonstrate how we might organize our code to prevent that.

    Each mapping method's code will be separated out into its own VariantService class, which will implement the IVariantService interface. It is better to be more organized.

Lets run the project and firstly see the OpenApi Interface.

And test it with adding and gathering some data.

    Final words... I try to demonstrated how to build a simple API using CRUD operations. You can find demo project source codes in my Github Repo which is here. And here is some other useful links that you might want to read.

See you in next article.

Hiç yorum yok:

Yorum Gönder

Bottom Ad [Post Page]