Anto Subash.

Published on

Minimal Api with ABP - Hello World - Part 1

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.

1dotnet new web -n HelloWorld

This will create a empty hello world project.

Let add the abp packages.

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

Minimal Module

lets create a very simple module.

1[DependsOn(
2    typeof(AbpAspNetCoreMvcModule),
3    typeof(AbpAutofacModule)
4)]
5public class MinimalModule : AbpModule
6{
7}

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.

1public class HelloService : ITransientDependency
2{
3    public string SayHi()
4    {
5        return "Hi from service";
6    }
7}

Minimal Application

Lets create the minimal application

1using Volo.Abp;
2using Microsoft.AspNetCore.Mvc;
3using Volo.Abp.AspNetCore.Mvc;
4using Volo.Abp.Autofac;
5using Volo.Abp.Modularity;
6using Volo.Abp.DependencyInjection;
7var builder = WebApplication.CreateBuilder(args);
8builder.Host.UseAutofac();
9builder.Services.ReplaceConfiguration(builder.Configuration);
10builder.Services.AddApplication<MinimalModule>();
11
12var app = builder.Build();
13
14app.MapGet("/hi", ([FromServices] HelloService helloService) =>
15{
16    return helloService.SayHi();
17});
18
19app.InitializeApplication();
20app.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.

1dotnet run

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