Anto Subash.

dotnet
abp

Minimal Api with ABP - Hello World - Part 1

Table of contents

Intro

Minimal api in .Net 6 provides a new way for creating http api. Minimal API provides simplicity and removes lot of boilerplate. We will see how to use the minimal api with ABP.

Hello world

First lets create a empty c# project.

dotnet new web -n HelloWorld

This will create a empty hello world project.

Let add the abp packages.

dotnet add package Volo.Abp.Autofac
dotnet add package Volo.Abp.AspNetCore.Mvc

Minimal Module

lets create a very simple module.

[DependsOn(
    typeof(AbpAspNetCoreMvcModule),
    typeof(AbpAutofacModule)
)]
public class MinimalModule : AbpModule
{
}

This module depends on AbpAspNetCoreMvcModule and AbpAutofacModule. The configure service method is used to register the assembly of MinimalModule for dependency injection.

Hello service

lets create a simple service which will say hello world.

public class HelloService : ITransientDependency
{
    public string SayHi()
    {
        return "Hi from service";
    }
}

Minimal Application

Lets create the minimal application

using Volo.Abp;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Autofac;
using Volo.Abp.Modularity;
using Volo.Abp.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
builder.Host.UseAutofac();
builder.Services.ReplaceConfiguration(builder.Configuration);
builder.Services.AddApplication<MinimalModule>();

var app = builder.Build();

app.MapGet("/hi", ([FromServices] HelloService helloService) =>
{
    return helloService.SayHi();
});

app.InitializeApplication();
app.Run();

This application has only one end point /hi which prints hello world. We are Injecting the hello world service we created and then we are calling the SayHi method from the service.

To run the app.

dotnet run

Repo: https://github.com/antosubash/AbpMinimalApiHelloWorld

Buy Me a Coffee at ko-fi.com