https://github.com/james231/minimalapi-withdi
Example of .NET 6 minimal APIs with Controllers and Dependency Injection
https://github.com/james231/minimalapi-withdi
api asp-net-core csharp minimal-api
Last synced: about 1 month ago
JSON representation
Example of .NET 6 minimal APIs with Controllers and Dependency Injection
- Host: GitHub
- URL: https://github.com/james231/minimalapi-withdi
- Owner: James231
- Created: 2021-09-20T20:19:01.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2021-09-26T15:12:27.000Z (almost 5 years ago)
- Last Synced: 2025-03-01T08:17:25.033Z (over 1 year ago)
- Topics: api, asp-net-core, csharp, minimal-api
- Language: C#
- Homepage:
- Size: 8.79 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# MinimalApi-WithDI
Example of .NET 6 ([Preview 7](https://devblogs.microsoft.com/dotnet/announcing-net-6-preview-7/) or later) [Minimal APIs](https://devblogs.microsoft.com/aspnet/asp-net-core-updates-in-net-6-preview-4/#introducing-minimal-apis) with Controllers and Dependency Injection.
## Eh? 🥴
Normally APIs built with ASP.NET include a lot of stuff out-of-the-box, which you might not need. And need a lot of boilerplate code to setup properly.
Minimal APIs added in .NET 6 gives you a barebones project template, with all that missing. You can just write a few simple lines of code to define route mappings to C# methods which return strings.
This project takes Minimal APIs and adds a bit more. Specifically, I've added Controllers for endpoints, which can be defined using an `[ApiController]` attribute, and dependency injection with equivalent of the usual `ConfigureServices` method.
## Adding Controllers
Look at the `Controllers` folder. Just add them there. Should be obvious.
## Adding Services to DI Container
Look at `Program.cs`, and add your services to `ConfigureServices` at the end.
## Ideas
- I might replace the reflection code with source generators one day. For nice and speedy startups 🚅
- Could try using the same codebase for Azure Functions?