ApiRoutes 1.0.1
dotnet add package ApiRoutes --version 1.0.1
NuGet\Install-Package ApiRoutes -Version 1.0.1
<PackageReference Include="ApiRoutes" Version="1.0.1" />
<PackageVersion Include="ApiRoutes" Version="1.0.1" />
<PackageReference Include="ApiRoutes" />
paket add ApiRoutes --version 1.0.1
#r "nuget: ApiRoutes, 1.0.1"
#addin nuget:?package=ApiRoutes&version=1.0.1
#tool nuget:?package=ApiRoutes&version=1.0.1
ApiRoutes
ApiRoutes is a lightweight library for simplifying the implementation of API routes and request handling in C# using Source Generators.
Features
- Simplifies the definition and handling of API routes.
- Generates route code using Source Generators for improved developer experience.
- Supports request/response patterns for handling API requests.
- Full support for NativeAOT with .NET 8.
- Zero Overhead. Same performance as Minimal API. No reflection at runtime.
CQRS Pattern
ApiRoutes follows the Command Query Responsibility Segregation (CQRS) pattern, which separates read and write operations into distinct parts. By adopting the CQRS pattern, you can achieve better scalability, performance, and maintainability in your applications.
The library encourages the use of the CQRS pattern by providing a clear separation between requests and handlers. Requests represent commands or queries, while handlers encapsulate the business logic to handle these requests.
Getting Started
Prerequisites
- .NET 7.0 or later
Installation
To use the ApiRoutes Library in your project, follow these steps:
Install the NuGet package into your project:
dotnet add package ApiRoutes
Define your request classes by creating classes that implement the IRequest<TResponse> interface and annotate them with the ApiRoute attribute. The ApiRoute attribute takes a route pattern and HttpMethod enum value from the library. For example:
[ApiRoute("/hello-world", Method.POST, ReadBody = true)] public class HelloWorldCommand : IRequest<string> { public string MyString { get; set; } }
Create the handlers for your requests by implementing the IHandler<TRequest, TResponse> interface. For example:
public class HelloWorldCommandHandler : IHandler<HelloWorldCommand, string> { public async ValueTask<RequestResult> InvokeAsync(HelloWorldCommand command, CancellationToken cancellationToken = default) { // Handle the request logic // Do not throw exceptions! Return them with this.Error(exception); // All exceptions should extend RequestException to specify their status code. return this.Ok("Hello World"); } }
In your ASP.NET Core Minimal API application, add the following code to configure the ApiRoutes Library:
var builder = WebApplication.CreateBuilder(args); // Add ApiRoutes services builder.Services.AddApiRoutes<{AssemblyNameHere}GeneratedRouteConfiguration>(); // ... var app = builder.Build(); // Map ApiRoutes app.MapApiRoutes(); // ...
Build and run your application.
For more detailed usage examples and advanced configurations, please refer to the examples directory in the project.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net7.0
- FluentValidation (>= 11.9.0)
- JetBrains.Annotations (>= 2023.2.0-eap3)
-
net8.0
- FluentValidation (>= 11.9.0)
- JetBrains.Annotations (>= 2023.2.0-eap3)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on ApiRoutes:
Package | Downloads |
---|---|
ApiRoutes.Swagger
Provides support for Swashbuckle.AspNetCore when using ApiRoutes |
|
ApiRoutes.EFCore
Provides support for Microsoft.EntityFrameworkCore when using ApiRoutes |
GitHub repositories
This package is not used by any popular GitHub repositories.