Creating RESTful Minimal WebAPI in .Net 6 in an Easy Manner! | FastEndpoints

 We might have developed Web APIs using MVC Controllers where we used to give Route Path Attributes to the Controller and  to identify individual methods we used to provide Actions in the Route.

With the Controller approach we need to write boiler plate code in separate files which deals with the Configuration of the Web Service. Those who have worked in Node js would be aware of how easy it is to create a Web API and expose different Endpoints without the need of so much boiler plate code.

Here we are going to discuss of similar approach for creating a Web API in .Net6 using FastEndpoints. Under the hood it uses Minimal API concept which tries to reduce the dependencies required to achieve a particular task. This approach is more suitable in the case of Microservice Architecture where we want to keep the dependencies to minimum.

Must Read : Fluent API and Data Annotations

What are FastEndpoints?

FastEndpoints is a Framework compatible with .Net 6 which allows to build a RESTful Web API. It implements Request-Endpoint-Response(REPR) pattern.

It is available as a Nuget Package

Advantages of using FastEndpoints

  • Faster when compared to traditional MVC Controllers
  • Uses less memory than the MVC counter part
  • Enhances developer productivity as there is no need to write Boiler plate code to configure WebAPI.

 Sample Application using FastEndPoints

Now we are going to see how to create a sample application using FastEndPoints framework.

  • For that First create a console Application with target framework as .Net 6.

Modify the Project type to <Project Sdk="Microsoft.NET.Sdk.Web"> so that it can be run as a Web Project.

  • Structure of the Application is as shown below:

View of Web API Project Structure in .Net 6


  • Following code demonstrates how to setup the Web Application 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using FastEndpoints;
using FastEndpoints.Swagger;
using Microsoft.AspNetCore.Builder;
using System;

namespace FastEndPoints
{
    class Program
    {
        static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder();
            builder.Services.AddFastEndpoints();
            builder.Services.AddSwaggerDoc();

            var app = builder.Build();
            app.UseOpenApi();
            app.UseAuthorization();
            app.UseFastEndpoints();
            app.UseSwaggerUi3(x=>x.ConfigureDefaults());
            app.Run();
        }
    }
}


  • Next Step is to define EndPoints and Request/Response Structures

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using FastEndpoints;
using FastEndPoints_WebAPI.Request;
using FastEndPoints_WebAPI.Response;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace FastEndPoints_WebAPI.EndPoints
{
    public class MonthEndPoint : Endpoint<MonthIndexRequest,MonthNamesResponse>
    {
        private IEnumerable<string> Months = new List<string>()
        {
            "JANUARY","FEBRAUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER"
        };

        public override void Configure()
        {
            Verbs(Http.GET);
            Routes("/months/{index}");
            AllowAnonymous();
        }

        public override async Task HandleAsync(MonthIndexRequest req, CancellationToken ct)
        {
            await SendAsync(new MonthNamesResponse()
            {
                MonthNames = (req.Index == 0) ? Months :  new List<string>() { Months.ElementAt(req.Index - 1) }
            });
        }
    }
}


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
namespace FastEndPoints_WebAPI.Request
{
    public class MonthIndexRequest
    {
        public int Index { get; set; }
    }
}

using System.Collections.Generic;

namespace FastEndPoints_WebAPI.Response
{
    public class MonthNamesResponse
    {
        public IEnumerable<string> MonthNames { get; set; }
    }
}

As we can see from the above code how simple it is to Define and Setup Endpoint. I have also added Swagger support in this Web API project to Test out the Web APIs

To know more about Swagger and how its integrated have a look at the post : Self Hosting ASP.Net Web Application

Following Screenshots show the results in Swagger page for the Web API developed using FastEndpoints

Swagger Output for Web API .Net 6

Swagger Output for Web API .Net 6

Swagger Doc for Web API in .Net 6





Comments

Popular posts from this blog

Mastering Concurrency with Latches and Barriers in C++20: A Practical Guide for Students

Graph Visualization using MSAGL with Examples