EndpointDefinition 1.0.3

dotnet add package EndpointDefinition --version 1.0.3
                    
NuGet\Install-Package EndpointDefinition -Version 1.0.3
                    
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="EndpointDefinition" Version="1.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EndpointDefinition" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="EndpointDefinition" />
                    
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 EndpointDefinition --version 1.0.3
                    
#r "nuget: EndpointDefinition, 1.0.3"
                    
#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.
#:package EndpointDefinition@1.0.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=EndpointDefinition&version=1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=EndpointDefinition&version=1.0.3
                    
Install as a Cake Tool

EndpointDefinition

NuGet Build & Publish License: MIT

A lightweight library for organizing and registering API endpoints in ASP.NET Core applications using a clean, modular approach.

Features

  • Modular Endpoint Organization: Define endpoints in separate classes for improved maintainability
  • Dependency Injection Support: Register services specific to each endpoint
  • Environment-aware Configuration: Configure endpoints differently based on environment
  • Automatic Registration: Easily scan and register all endpoint definitions in your assemblies
  • Parallel Endpoint Registration: Improves startup performance for applications with many endpoints

Installation

Install the package via NuGet Package Manager:

dotnet add package EndpointDefinition

Or via the Package Manager Console:

Install-Package EndpointDefinition

Usage

Step 1: Create Endpoint Definition Classes

Create classes that implement the IEndpointDefinition interface:

using EndpointDefinition;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Hosting;

namespace YourNamespace.Endpoints
{
    public class WeatherEndpoints : IEndpointDefinition
    {
        public void DefineServices(IServiceCollection services)
        {
            // Register services required by this endpoint
            services.AddScoped<IWeatherService, WeatherService>();
        }

        public void DefineEndpoints(WebApplication app, IWebHostEnvironment env)
        {
            // Define endpoints
            app.MapGet("/weather", (IWeatherService weatherService) => 
            {
                return weatherService.GetForecast();
            })
            .WithName("GetWeatherForecast")
            .WithOpenApi();
            
            // Define different endpoints based on environment
            if (env.IsDevelopment())
            {
                app.MapGet("/weather/debug", () => "Debug endpoint");
            }
        }
    }
}

Step 2: Register Endpoint Definitions in Program.cs

Register and use the endpoint definitions in your Program.cs file:

using EndpointDefinition;
using YourNamespace.Endpoints;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container
builder.Services.AddEndpointDefinitions(typeof(Program));
// Or specify multiple marker types:
// builder.Services.AddEndpointDefinitions(typeof(Program), typeof(WeatherEndpoints));

var app = builder.Build();

// Use the endpoint definitions
app.UseEndpointDefinitions(app.Environment);

app.Run();

Advanced Usage

Organizing Endpoints by Feature

Create separate endpoint definition classes for different features:

public class UserEndpoints : IEndpointDefinition
{
    public void DefineServices(IServiceCollection services)
    {
        services.AddScoped<IUserRepository, UserRepository>();
        services.AddScoped<IUserService, UserService>();
    }

    public void DefineEndpoints(WebApplication app, IWebHostEnvironment env)
    {
        var group = app.MapGroup("/users").WithTags("Users");
        
        group.MapGet("/", (IUserService userService) => userService.GetAllUsers());
        group.MapGet("/{id}", (int id, IUserService userService) => userService.GetUserById(id));
        group.MapPost("/", (UserCreateDto user, IUserService userService) => userService.CreateUser(user));
        // ...
    }
}

Conditional Endpoint Registration

Register endpoints based on specific conditions:

public class AdminEndpoints : IEndpointDefinition
{
    public void DefineServices(IServiceCollection services)
    {
        services.AddScoped<IAdminService, AdminService>();
    }

    public void DefineEndpoints(WebApplication app, IWebHostEnvironment env)
    {
        // Only register these endpoints in non-production environments
        if (!env.IsProduction())
        {
            var group = app.MapGroup("/admin").WithTags("Admin").RequireAuthorization("AdminOnly");
            
            group.MapGet("/statistics", (IAdminService adminService) => adminService.GetStatistics());
            group.MapPost("/reset-data", (IAdminService adminService) => adminService.ResetData());
        }
    }
}

Contributing

Contributions are welcome! Here's how you can contribute:

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/your-feature-name
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin feature/your-feature-name
  5. Submit a pull request

Versioning

This project uses SemVer for versioning. The version is automatically updated in the CI/CD pipeline when changes are made to the project file.

CI/CD

This project uses GitHub Actions for continuous integration and deployment. The workflow automatically:

  • Builds and tests the project
  • Packs the library into a NuGet package
  • Publishes the package to NuGet.org when the version is updated
  • Creates a GitHub release with the version tag

License

This project is licensed under the MIT License - see the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET 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.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.3 160 3/18/2025
1.0.2 153 3/17/2025
1.0.1 140 3/17/2025