Building High-Performance .NET Core Web APIs with FastEndpoints

Valentyn Osidach đź“š
.Net Programming
Published in
4 min readAug 21, 2024

Introduction

Performance is key for any developer or organization in this fast-moving scenery of web development. At the core of modern applications are APIs, whose demand is greater than ever for speedy and reliable responses. FastEndpoints is a lightweight framework designed to ease and speed up the development of High-Performance .NET Core Web APIs.

The following article will describe what FastEndpoints is, why it is an interesting choice for building APIs, and how to get started with this package in your .NET Core projects.

What is FastEndpoints?

FastEndpoints is a minimalist framework built on top of ASP.NET Core, offering a streamlined approach to building Web APIs. It aims to reduce boilerplate code and enhance performance, making it easier and quicker to develop APIs without sacrificing flexibility or power.

FastEndpoints introduces a different approach to handling API requests compared to traditional controllers in ASP.NET Core. It leverages the concept of endpoints, which are specialized classes that encapsulate the request-handling logic, including validation, authentication, and response generation.

Why Use FastEndpoints?

FastEndpoints offers several benefits that make it an attractive choice for building .NET Core Web APIs:

  1. Simplicity: FastEndpoints promotes a clear and concise structure for your API logic, reducing the need for boilerplate code and unnecessary abstractions.
  2. Performance: With a focus on minimizing overhead, FastEndpoints is designed to deliver high-performance APIs that can handle a large number of requests efficiently.
  3. Modularity: FastEndpoints encourages modular design, making it easy to organize and maintain your codebase. Each endpoint is self-contained, leading to better separation of concerns.
  4. Built-in Features: FastEndpoints comes with built-in support for common API requirements, such as request validation, authentication, and error handling, allowing you to focus on business logic.

Getting Started with FastEndpoints

Let’s walk through the process of setting up a .NET Core Web API project using FastEndpoints.

1. Setting Up the Project

First, you need to create a new ASP.NET Core Web API project. You can do this using the .NET CLI:

dotnet new webapi -n FastEndpointsDemo

2. Next, install the FastEndpoints NuGet package

dotnet add package FastEndpoints

3. Define Your First Endpoint

With FastEndpoints, you define API endpoints as classes that inherit from the Endpoint class. Let's create a simple example where we define an endpoint to handle HTTP GET requests for fetching a list of products.

Create a new folder called Endpoints, and inside it, create a file named GetProductsEndpoint.cs:

using FastEndpoints;

public class GetProductsEndpoint : EndpointWithoutRequest<List<Product>>
{
public override void Configure()
{
Verbs(Http.GET);
Routes("/products");
AllowAnonymous();
}

public override async Task HandleAsync(CancellationToken ct)
{
var products = new List<Product>
{
new Product { Id = 1, Name = "Laptop", Price = 1200.99M },
new Product { Id = 2, Name = "Smartphone", Price = 699.99M }
};

await SendAsync(products);
}
}

public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}

In this example:

  • EndpointWithoutRequest<List<Product>> indicates that this endpoint doesn’t expect a request body but will return a list of Product objects.
  • The Configure method is used to specify the HTTP verb, route, and other settings.
  • The HandleAsync method contains the logic for handling the request and sending the response.

4. Register FastEndpoints

To use FastEndpoints, you need to register it in your Program.cs file.

var builder = WebApplication.CreateBuilder(args);

// Register FastEndpoints
builder.Services.AddFastEndpoints();

var app = builder.Build();

// Use FastEndpoints
app.UseFastEndpoints();

app.Run();

Advanced Features

FastEndpoints offers a range of features that allow you to build more complex APIs efficiently:

  • Request Validation: FastEndpoints allows you to easily add validation logic to your endpoints. You can define validation rules using FluentValidation or custom logic.
  • Authentication & Authorization: FastEndpoints integrates seamlessly with ASP.NET Core’s authentication and authorization mechanisms, allowing you to secure your APIs with minimal configuration.
  • Middleware: You can add custom middleware to your endpoints, providing a powerful way to inject additional logic into the request pipeline.
  • Error Handling: FastEndpoints simplifies error handling by providing built-in mechanisms to handle exceptions and return standardized error responses.

Conclusion

FastEndpoints is a powerful tool for building high-performance, modular, and easy-to-maintain Web APIs in .NET Core. By reducing boilerplate code and offering a clean, concise approach to defining endpoints, it allows developers to focus on writing business logic rather than dealing with repetitive infrastructure concerns.

Whether you are building a small microservice or a large-scale API, FastEndpoints can help you streamline your development process while ensuring that your APIs are fast and reliable. If you’re looking to speed up your .NET Core API development while maintaining high performance, FastEndpoints is definitely worth exploring.

--

--

.Net Programming
.Net Programming

Published in .Net Programming

Explore the .Net with the latest in C# from basic to advanced, including .Net versions 9, 8, 6, 5, Core 3.1, .Net Framework, ASP.NET Core, MVC, design patterns, OOPS, and SOLID principles. Get top tutorials, best practices, and hands-on code examples on GitHub.

Valentyn Osidach đź“š
Valentyn Osidach đź“š

Written by Valentyn Osidach đź“š

I am a software developer from Ukraine with more than 7 years of experience. I specialize in C# and .NET and love sharing my development knowledge. 👨🏻‍💻