A modern, controller-based ASP.NET Core Web API project implementing a Todo application with full CRUD operations. This project serves as a comprehensive example of building RESTful APIs using ASP.NET Core best practices.
This project demonstrates how to build a robust Web API using ASP.NET Core, featuring:
- Controller-based architecture for organized endpoint management
- Entity Framework Core with In-Memory database for development
- Complete CRUD operations (Create, Read, Update, Delete)
- Swagger/OpenAPI integration for API documentation and testing
- HTTP files for endpoint testing in Visual Studio
- Professional project structure following .NET conventions
Technology | Version | Purpose |
---|---|---|
ASP.NET Core | 9.0 | Web API framework |
Entity Framework Core | 9.0 | Data access layer |
C# | 12.0 | Programming language |
Visual Studio | 2022+ | IDE |
- .NET 9.0 SDK or later
- Visual Studio 2022 or Visual Studio Code
- Git (for cloning)
-
Clone the repository:
git clone https://github.com/your-username/aspnetcore-todo-api.git cd aspnetcore-todo-api
-
Restore dependencies:
dotnet restore
-
Build the project:
dotnet build
-
Run the application:
dotnet run
-
Access the API:
- Swagger UI:
https://localhost:7xxx/swagger
(opens automatically) - HTTP requests: Open
TodoApi.http
in Visual Studio and send requests
- Swagger UI:
https://localhost:7xxx/api/todoitems
Method | Endpoint | Description | Request Body | Response |
---|---|---|---|---|
GET |
/todoitems |
Get all todo items | None | Array of TodoItem |
GET |
/todoitems/{id} |
Get item by ID | None | TodoItem object |
POST |
/todoitems |
Create new item | TodoItem JSON | Created TodoItem |
PUT |
/todoitems/{id} |
Update existing item | TodoItem JSON | Updated TodoItem |
DELETE |
/todoitems/{id} |
Delete item | None | 204 No Content |
{
"id": 1,
"name": "Walk the dog",
"isComplete": false
}
POST /api/todoitems
Content-Type: application/json
{
"name": "Learn ASP.NET Core",
"isComplete": false
}
PUT /api/todoitems/1
Content-Type: application/json
{
"id": 1,
"name": "Learn ASP.NET Core Web API",
"isComplete": true
}
GET /api/todoitems
- Run the application
- Navigate to
https://localhost:7xxx/swagger
- Expand endpoints and click "Try it out"
- Fill in parameters and click "Execute"
- Open
TodoApi.http
in Visual Studio 2022+ - Click "Send Request" above each HTTP request
- View responses in the output window
# Get all items
curl -X GET "https://localhost:7xxx/api/todoitems"
# Create new item
curl -X POST "https://localhost:7xxx/api/todoitems" \
-H "Content-Type: application/json" \
-d '{"name":"Test Item","isComplete":false}'
This project uses Entity Framework Core In-Memory Database for simplicity. To switch to a persistent database:
- Install the appropriate EF Core provider (SQL Server, PostgreSQL, etc.)
- Update
Program.cs
to use the new provider - Add connection string to
appsettings.json
CORS is configured to allow all origins in development. For production:
builder.Services.AddCors(options =>
{
options.AddPolicy("ProductionPolicy",
builder => builder.WithOrigins("https://yourdomain.com")
.AllowAnyHeader()
.AllowAnyMethod());
});
Microsoft.AspNetCore.OpenApi
- OpenAPI/Swagger supportMicrosoft.EntityFrameworkCore.InMemory
- In-memory database providerSwashbuckle.AspNetCore
- Swagger UI integration
Microsoft.AspNetCore.Mvc.Testing
- Integration testingMicrosoft.EntityFrameworkCore.Tools
- EF Core CLI tools
- Make changes to controllers or models
- Test locally using Swagger UI or HTTP files
- Run tests (if implemented)
- Commit changes with descriptive messages
- Push to repository
Consider implementing these enhancements:
- Authentication & Authorization (JWT, Identity)
- Persistent Database (SQL Server, PostgreSQL)
- Unit & Integration Tests
- Logging (Serilog, NLog)
- Caching (Redis, In-Memory)
- API Versioning
- Rate Limiting
- Docker Support
- CI/CD Pipeline
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Built with β€οΈ using ASP.NET Core