English | Português
A .Net 9.0
WebApi boilerplate / template project. MediatR, Swagger, AutoMapper Mapster, Serilog and more implemented.
The goal of this project is to be a kickstart to your .Net WebApi, implementing the most common used patterns and technologies for a restful API in .net, making your work easier.
This project is now configured as a .NET template that you can install and use to create new projects with customizable names and settings.
-
From this repository:
git clone <this-repo-url> cd dot-net-api-template dotnet new install .
-
Verify installation:
dotnet new list # Look for ".NET Web API Boilerplate" with short name "webapi-boilerplate"
# Create a new project with default settings
dotnet new webapi-boilerplate -n "MyAwesomeApi"
cd MyAwesomeApi
# Create with custom configuration
dotnet new webapi-boilerplate \
-n "MyCompanyApi" \
--CompanyName "MyCompany" \
--Port0 8080 \
--Port1 8081 \
--EnableDocker true \
--EnableSwagger true
Parameter | Type | Default | Description |
---|---|---|---|
ProjectName |
string | "MyWebApi" | The name of your project (replaces "Boilerplate") |
CompanyName |
string | "MyCompany" | Your company/organization name |
Port0 |
integer | 7122 | HTTP port number for the API |
Port1 |
integer | 7123 | HTTPS port number for the API |
EnableSwagger |
bool | true | Include Swagger/OpenAPI documentation |
EnableDocker |
bool | true | Include Docker support files |
When you create a new project using this template:
- ✅ All namespaces will use your
ProjectName
- ✅ Solution and project files will be properly renamed
- ✅ Docker configurations will use your project name
- ✅ Port numbers will be customized if specified
- ✅ Company references will be updated throughout
- ✅ All "Boilerplate" references will be replaced
dotnet new uninstall .
- Download the latest .Net SDK and Visual Studio/Code/Rider.
- You may need a running instance of Postgres, with appropriate migrations initialized.
- You can run just the DB on docker. For that, run the following command:
docker-compose up -d db-server
. Doing that, the application will be able to reach the container of the db server.
- You can run just the DB on docker. For that, run the following command:
- Go to the src/YourProjectName.Api folder and run
dotnet run
, or, in visual studio set the api project as startup and run as console/docker/IIS. - Visit the URLs shown in your terminal or the ports you configured to access the application's swagger.
- Run
docker-compose up -d
in the root directory, or, in visual studio, set the docker-compose project as startup and run. This should start the application and DB.
-
- For docker-compose, you should run this command on the root folder:
dotnet dev-certs https -ep https/aspnetapp.pfx -p yourpassword
Replace "yourpassword" with something else in this command and the docker-compose.override.yml file. This creates the https certificate.
- For docker-compose, you should run this command on the root folder:
- Visit the URLs shown in your terminal or the ports you configured to access the application's swagger.
Important: You need to have docker up and running. The integration tests will launch a Postgres container and use it to test the API.
In the root folder, run dotnet test
. This command will try to find all test projects associated with the sln file.
If you are using Visual Studio, you can also access the Test Menu and open the Test Explorer, where you can see all tests and run all of them or one specifically.
In this project, some routes requires authentication/authorization. For that, you will have to use the api/identity/register
route to create an account.
After that, you can login using the /api/identity/login
without using cookies and then use received accessToken on the lock (if using swagger) or via the Authorization header on a http request.
For more information, please take a look on swagger documentation.
- SwaggerUI
- EntityFramework
- Postgres
- Minimal apis
- Strongly Typed Ids
- Test coverage collection
AutoMapperMapster- MediatR
- Feature slicing
- Serilog with request logging and easily configurable sinks
- .Net Dependency Injection
- Resource filtering
- Response compression
- Response pagination
- CI (Github Actions)
- Authentication
- Authorization
- Unit tests
- Integration tests with testcontainers
- Container support with docker and docker-compose
- OpenTelemetry support (with OLTP as default exporter)
- NuGet Central package management (CPM)
- Services
- This folder stores your apis and any project that sends data to your users.
- Boilerplate.Api
- This is the main api project. Here are all the controllers and initialization for the api that will be used.
- docker-compose
- This project exists to allow you to run docker-compose with Visual Studio. It contains a reference to the docker-compose file and will build all the projects dependencies and run it.
- Application
- This folder stores all data transformations between your api and your domain layer. It also contains your business logic.
- Auth
- This folder contains the login Session implementation.
- Domain
- This folder contains your business models, enums and common interfaces.
- Boilerplate.Domain
- Contains business models and enums.
- Auth
- This folder contains the login Session Interface.
- Infra
- This folder contains all data access configuration, database contexts, anything that reaches for outside data.
- Boilerplate.Infrastructure
- This project contains the dbcontext, entities configuration and migrations.
- Remove/Rename all hero related stuff to your needs.
- Update business logic, entities, and API endpoints for your domain.
- Configure your database connection strings and environment variables.
- Customize Docker settings if needed.
- Give this repo a star!
To run migrations on your generated project, you need the dotnet-ef tool.
- Run
dotnet tool install --global dotnet-ef
- Now, depending on your OS, you have different commands (replace "YourProjectName" with your actual project name):
- For windows:
dotnet ef migrations add InitialCreate --startup-project .\src\YourProjectName.Api\ --project .\src\YourProjectName.Infrastructure\
- For linux/mac:
dotnet ef migrations add InitialCreate --startup-project ./src/YourProjectName.Api/ --project ./src/YourProjectName.Infrastructure/
- For windows:
If this template was useful for you, or if you learned something, please give it a Star! ⭐
This project has great influence of https://github.com/lkurzyniec/netcore-boilerplate and https://github.com/EduardoPires/EquinoxProject. If you have time, please visit these repos, and give them a star, too!
This boilerplate/template was developed by Yan Pitangui under MIT license.