Creating API Controllers
Controllers expose your use cases through HTTP endpoints. This architecture provides a standardized approach to API responses and error handling.Overview
Every controller must:- Inherit from
BaseController - Use dependency injection for
ICommandQueryBus - Follow RESTful conventions
- Return standardized responses
Step-by-Step Guide
Create the Controller Class
Create a new controller in Example from codebase:
Template-API/Controllers/YourEntityController.cs:Template-API/Controllers/DummyEntityController.cs:19-21Implement GET All Endpoint
Add a GET endpoint with pagination support:Example from codebase: Response:
Template-API/Controllers/DummyEntityController.cs:23-29Request:Implement GET by ID Endpoint
Add a GET endpoint for retrieving a single entity:Example from codebase: Response:
Template-API/Controllers/DummyEntityController.cs:31-39Request:Implement POST Endpoint
Add a POST endpoint for creating entities:Example from codebase: Response:Status:
Template-API/Controllers/DummyEntityController.cs:41-49Request:201 CreatedLocation Header: api/YourEntity/123e4567-e89b-12d3-a456-426614174000Implement PUT Endpoint
Add a PUT endpoint for updating entities:Example from codebase: Response: Empty bodyStatus:
Template-API/Controllers/DummyEntityController.cs:51-59Request:204 No ContentBaseController Features
TheBaseController provides standardized response formatting.
Standard Response Format
All successful responses are wrapped in a standard format:Template-API/Controllers/BaseController.cs:30-37
Automatic Response Wrapping
When you callOk(data), the response is automatically wrapped:
Template-API/Controllers/BaseController.cs:12-27
Error Handling with BaseExceptionFilter
All exceptions are automatically handled byBaseExceptionFilter.
Exception Mapping
Template-API/Filters/BaseExceptionFilter.cs:34-60
Error Response Format
Registering the Filter
The filter is registered inStartup.cs:
Template-API/Startup.cs:29-32
Complete Controller Example
Here’s the completeDummyEntityController from the codebase:
Template-API/Controllers/DummyEntityController.cs
Best Practices
RESTful Routing
RESTful Routing
Follow these conventions:
GET /api/v1/[Controller]- List all (with pagination)GET /api/v1/[Controller]/{id}- Get singlePOST /api/v1/[Controller]- CreatePUT /api/v1/[Controller]- UpdateDELETE /api/v1/[Controller]/{id}- Delete
[Controller] token for automatic controller name replacement.Validation
Validation
Perform basic validation in the controller:Domain validation is handled by entity validators and caught by
BaseExceptionFilter.Return Types
Return Types
Use appropriate HTTP status codes:
Ok(data)- 200 OK (GET, successful query)Created(location, data)- 201 Created (POST)NoContent()- 204 No Content (PUT, DELETE)BadRequest()- 400 Bad Request (validation failure)- Exceptions are mapped automatically by the filter
Async/Await
Async/Await
Always use async/await for database operations:
Testing Your API
Using Swagger
The API includes Swagger UI for testing:- Run the application
- Navigate to
https://localhost:<port>/swagger - Test endpoints interactively
Using cURL
Next Steps
Database Setup
Configure database connections and migrations
Docker Deployment
Deploy your API using Docker