FastEndpoints in C# .NET
FastEndpoints is a framework that helps you build APIs faster by reducing boilerplate code and streamlining the development process.
In this blog, we’ll go through the entire process of setting up a FastEndpoint project, creating endpoints, and testing them.
Let’s build a basic API using FastEndpoints in .NET. Follow these steps:
Step 1: Create a New Project
- Open your terminal/command prompt.
- Run:
dotnet new web -n FastEndpointsDemo
cd FastEndpointsDemo
Step 2: Install FastEndpoints
Add the FastEndpoints NuGet package:
dotnet add package FastEndpoints
Step 3: Set Up the Project
- Open
Program.cs
and replace the code with:
using FastEndpoints;
var builder = WebApplication.CreateBuilder();
builder.Services.AddFastEndpoints(); // Add FastEndpoints services
var app = builder.Build();
app.UseFastEndpoints(); // Enable FastEndpoints
app.Run();
Step 4: Create Your First Endpoint
- Create a folder named
Endpoints
in your project. - Add a new file
HelloWorldEndpoint.cs
insideEndpoints
:
using FastEndpoints;
public class HelloWorldEndpoint : EndpointWithoutRequest
{
public override void Configure()
{
Get("/hello"); // Define route and HTTP method
AllowAnonymous(); // Allow unauthenticated access
}
public override async Task HandleAsync(CancellationToken ct)
{
await SendAsync(new { Message = "Hello, World!" });
}
}
Step 5: Test the Endpoint
- Run the project:
dotnet run
2. Open http://localhost:5000/hello
in your browser or Postman.
You’ll see:
{ "Message": "Hello, World!" }
Step 6: Add a POST Endpoint
Let’s create an endpoint that accepts data.
- Create a
Models
folder and addUserRequest.cs
:
public class UserRequest
{
public string Name { get; set; }
public int Age { get; set; }
}
2. Create CreateUserEndpoint.cs
in the Endpoints
folder:
using FastEndpoints;
public class CreateUserEndpoint : Endpoint<UserRequest>
{
public override void Configure()
{
Post("/users");
AllowAnonymous();
}
public override async Task HandleAsync(UserRequest req, CancellationToken ct)
{
var response = new
{
req.Name,
req.Age,
CreatedAt = DateTime.UtcNow
};
await SendAsync(response);
}
}
3. Test with Postman:
- URL:
POST http://localhost:5000/users
- Body (JSON):
{ "Name": "John", "Age": 30 }
- Response:
{ "Name": "John", "Age": 30, "CreatedAt": "2023-10-01T12:00:00Z" }
Step 7: Add Validation (Optional)
FastEndpoints supports FluentValidation.
- Install the validation package:
dotnet add package FastEndpoints.Swagger
2. Modify UserRequest.cs
to include validation:
using FluentValidation;
public class UserRequest
{
public string Name { get; set; }
public int Age { get; set; }
}
public class UserRequestValidator : Validator<UserRequest>
{
public UserRequestValidator()
{
RuleFor(x => x.Name).NotEmpty().WithMessage("Name is required!");
RuleFor(x => x.Age).GreaterThan(0).WithMessage("Age must be positive!");
}
}
3. The endpoint will now automatically validate incoming requests.
In Summary: Why Choose FastEndpoints?
- Reduced boilerplate code and less time spent on repetitive tasks.
- Better organization of your API code by separating endpoints into distinct classes.
- Performance and simplicity with built-in features like request validation and automatic routing.
- Faster development of clean, maintainable APIs.
FastEndpoints is a great tool for creating APIs quickly and efficiently, especially if you’re working on smaller or medium-sized projects where simplicity and speed are key.