Building ASP.NET Core APIs: Choose Minimal APIs, Controllers, and Middleware
When building an ASP.NET Core API, choose Minimal APIs or controllers first, then align service registrations and middleware order. Minimal APIs are a compact starting point for straightforward HTTP APIs; controllers can suit applications that rely on MVC features, complex organization, or established team conventions. Pipeline order and dependency-injection lifetimes affect behavior and tests.
When building an ASP.NET Core API, choose Minimal APIs or controllers first, then align service registrations and middleware order. Minimal APIs are a compact starting point for straightforward HTTP APIs; controllers can suit applications that rely on MVC features, complex organization, or established team conventions. Pipeline order and dependency-injection lifetimes affect behavior and tests.
This guide follows ASP.NET Core 10.0 documentation. Examples show structure, not a complete authentication, persistence, or deployment setup.
Choose an API style
Minimal APIs map routes and handlers in Program.cs. Controllers group endpoints in ControllerBase classes. Decide based on shared features and boundaries: a few independent routes may stay clearer as Minimal APIs; shared MVC features or filters may favor controllers. Mixed use is possible, but set conventions so route and policy ownership stays clear.
Register and inject services
Register implementations in builder.Services and receive them through constructor or handler parameters. The built-in container creates and disposes services. Choose lifetime deliberately: Singleton is shared for the application lifetime and must not retain request state; Scoped often suits per-request work; Transient creates a new instance when requested. Avoid a singleton capturing a scoped service.
Understand middleware order
Middleware can act before and after the next component. Use can pass control onward; Run is terminal and prevents later middleware from running. Exception handling usually needs to be early to catch later failures. Follow documented ordering for HTTPS redirection, routing, authentication, authorization, and endpoint mapping. Authorization needs the identity established by authentication.
Separate settings by environment
ASP.NET Core providers read appsettings.json, environment variables, command-line arguments, and secret stores. Keep development and deployment settings distinct. Never commit passwords or tokens; inject secrets through a local secret store or deployment management. For unexpected values, check the active environment, providers, precedence, content root, and working directory. Do not log secrets.
Test critical integration paths
Unit tests quickly cover isolated logic. Integration tests use a test host and client to check the request pipeline with infrastructure such as databases or files. Focus them on important read/write flows and boundaries; use unit tests for routine branches. Use test settings, isolate and clean test data, and verify the relevant response contract: status, body, headers, or persisted data.
Troubleshoot in order: route and HTTP method; middleware order and terminal Run calls; DI registrations and lifetimes; environment and providers; then narrow slow integration tests or improve cleanup.
References: Microsoft Learn ASP.NET Core 10.0 documentation on APIs, dependency injection, middleware, configuration, and integration tests.
References
[ASP.NET Core 10.0: APIs overview](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/apis?view=aspnetcore-10.0)
[ASP.NET Core 10.0: Dependency injection](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-10.0)
[ASP.NET Core 10.0: Middleware](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-10.0)
[ASP.NET Core 10.0: Configuration](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-10.0)
[ASP.NET Core 10.0: Integration tests](https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-10.0)