ApiRoutes 1.0.1

dotnet add package ApiRoutes --version 1.0.1
                    
NuGet\Install-Package ApiRoutes -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ApiRoutes" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ApiRoutes" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="ApiRoutes" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ApiRoutes --version 1.0.1
                    
#r "nuget: ApiRoutes, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=ApiRoutes&version=1.0.1
                    
Install ApiRoutes as a Cake Addin
#tool nuget:?package=ApiRoutes&version=1.0.1
                    
Install ApiRoutes as a Cake Tool

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:

  1. Install the NuGet package into your project:

    dotnet add package ApiRoutes
    
    
  2. 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; }
    }
    
  3. 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");
         }
    }
    
  4. 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();
    
    // ...
    
  5. Build and run your application.

For more detailed usage examples and advanced configurations, please refer to the examples directory in the project.

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.

Version Downloads Last updated
1.0.1 632 1/7/2024
1.0.0 165 1/7/2024