BoxCleanArchitectureApiTemplate 1.0.0

dotnet add package BoxCleanArchitectureApiTemplate --version 1.0.0
                    
NuGet\Install-Package BoxCleanArchitectureApiTemplate -Version 1.0.0
                    
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="BoxCleanArchitectureApiTemplate" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BoxCleanArchitectureApiTemplate" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="BoxCleanArchitectureApiTemplate" />
                    
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 BoxCleanArchitectureApiTemplate --version 1.0.0
                    
#r "nuget: BoxCleanArchitectureApiTemplate, 1.0.0"
                    
#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 BoxCleanArchitectureApiTemplate@1.0.0
                    
#: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=BoxCleanArchitectureApiTemplate&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=BoxCleanArchitectureApiTemplate&version=1.0.0
                    
Install as a Cake Tool

BoxCleanArchitectureApiTemplate

🌟 Overview

BoxCleanArchitectureApiTemplate is a robust ASP.NET Core Web API template built with strong Clean Architecture principles. It's designed to help developers quickly kickstart high-quality, testable, maintainable, and scalable API applications for production environments. This template integrates best practices and essential libraries to accelerate your development process.

✨ Features

  • Clean Architecture Principles: Clear separation of layers (Domain, Application, Infrastructure, Presentation) for enhanced maintainability and testability.
  • ASP.NET Core 8.0: Built on the latest .NET 8.0 SDK.
  • JWT (JSON Web Token) Authentication: Secure authentication and authorization system using JWT for API endpoints.
  • Dynamic IP Whitelist Middleware: Middleware to restrict API access based on allowed IP addresses, managed dynamically.
  • Dynamic API Access Restrictions: A system to define time-based access restrictions for specific API Endpoints, manageable from the database.
  • Swagger/OpenAPI Integration: Interactive API documentation via Swagger UI for easy testing and understanding of API endpoints.
  • Robust Error Handling: Centralized error handling for API responses.
  • Entity Framework Core: ORM for data management with SQL Server databases.
  • Centralized Logging: Configured logging for easy monitoring and troubleshooting.
  • MediatR: Utilized for CQRS (Command Query Responsibility Segregation) pattern and handling requests/responses.
  • FluentValidation: For robust data validation.

🚀 Getting Started

1. Install the Template

Ensure you have .NET SDK version 8.0 or newer installed. Then, run the following command to install the template from the NuGet Gallery:

dotnet new install BoxCleanArchitectureApiTemplate::*

2. Create a New Project from the Template

Navigate to the folder where you want to create your new project, then run the command:

dotnet new Boxcleanapi -n MyNewAwesomeApi

After that cd [YourProjectName]

dotnet restore
dotnet build
  • Replace YourProjectName with your desired project name.
  • The template will automatically create the solution and all sub-projects, setting up namespaces and project references correctly.

3. Database Setup

  • Open the Solution (YourProjectName.sln) in Visual Studio.

  • Open the appsettings.json file in your main project (YourProjectName).

  • Update the ConnectionStrings to point to your SQL Server instance:

    "ConnectionStrings": {
      "DefaultConnection": "Server=YourServerName;Database=YourDatabaseName;User Id=YourUser;Password=YourPassword;TrustServerCertificate=True"
    },
    
  • Run EF Core Migrations to create the database and schema:

    # Open Terminal/PowerShell in Visual Studio or Command Prompt
    # Navigate to the YourProjectName.Infrastructure folder
    cd YourProjectName.Infrastructure
    dotnet ef database update
    

4. Run the Application

  • Set your YourProjectName project as the Startup Project in Visual Studio.
  • Press F5 or click the "Run" button in Visual Studio.
  • The application will start and automatically open the Swagger UI in your browser.

🔑 Security & Configuration

JWT Authentication

  • Once the application is running, access the Swagger UI.
  • Navigate to the Login API endpoint (e.g., /api/Account/Login).
  • Obtain a JWT Token.
  • Click the "Authorize" button in the top right corner of the Swagger UI.
  • In the "Value" field, type Bearer (with a space after Bearer) and paste your JWT Token immediately after it.
  • Click "Authorize" to apply the token for authenticated API calls.
  • Important: Ensure the token you receive has a ClaimTypes.Role or role with a value like "Admin" or an appropriate role to access restricted endpoints.

Dynamic IP Whitelist

  • The IP Whitelist Middleware checks the IP Address of the client accessing the API.
  • You can manage allowed IPs in the IpWhitelist table in your database.
  • Testing: Try adding your own IP address to the IpWhitelist table to allow API access. If there are no IP entries in the table, or your IP is on the allowed list, access will be granted.

Dynamic API Access Restrictions

  • API Access Restrictions are managed in the ApiAccessRestrictions table in the database.
  • You can define ApiPath, HttpMethod, RestrictionStartTime, RestrictionEndTime, IsActive, and Reason for each restriction.
  • This middleware checks if an API call falls within a restricted time frame.
  • Testing: Try adding an entry to the ApiAccessRestrictions table to restrict access to a specific API endpoint during certain hours.

📂 Project Structure

The project is structured following Clean Architecture principles:

  • YourProjectName (Presentation Layer):
    • The main ASP.NET Core Web API project.
    • Contains Controllers, Middlewares (e.g., IpWhitelistMiddleware), Startup Configuration, and the Program entry point.
  • YourProjectName.Application (Application Layer):
    • Holds the core business logic of the application.
    • Includes Commands, Queries, Handlers, Interfaces for Application Services, and Validations.
    • References YourProjectName.Domain.
  • YourProjectName.Domain (Domain Layer):
    • The heart of the business logic, independent of other layers.
    • Contains Entities, Value Objects, Aggregates, Domain Events, and Repository Interfaces.
  • YourProjectName.Infrastructure (Infrastructure Layer):
    • Implements the Interfaces defined in the Domain and Application Layers.
    • Handles Database concerns (EF Core), External Services, and Third-party Libraries.
    • References YourProjectName.Domain.
  • YourProjectName.TestAPI (Integration/Functional Tests):
    • A project dedicated to Integration or Functional Tests.

🤝 Contributing

If you have suggestions or encounter issues, please contact me.

📄 License

This template is licensed under the MIT License.


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.

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.0 626 7/23/2025

Initial release of the Clean Architecture Web API Template.