ASP.NET Core API Playground - Project Guide
Project Overview
Build a comprehensive API playground demonstrating all Fundamentals concepts in a single application.
Project Structure
AspNetCorePlayground/
├── Models/
│ ├── Product.cs
│ ├── Category.cs
│ └── ApiResponse.cs
├── Services/
│ ├── IProductService.cs
│ ├── ProductService.cs
│ └── CacheService.cs
├── Middleware/
│ ├── RequestLoggingMiddleware.cs
│ ├── ApiKeyMiddleware.cs
│ └── ExceptionHandlingMiddleware.cs
├── Validators/
│ └── ProductValidator.cs
├── Endpoints/
│ ├── ProductEndpoints.cs
│ └── CategoryEndpoints.cs
├── Controllers/
│ └── ProductsController.cs
├── appsettings.json
└── Program.cs
Features to Implement
1. Minimal API Endpoints
- Product CRUD operations
- Category management
- Search and filtering
- Pagination
2. Controller-Based Endpoints
- Product management using controllers
- Demonstrate attribute routing
- Model validation with data annotations
3. Dependency Injection
- Register services with all three lifetimes
- Demonstrate proper usage patterns
- Show captive dependency problem
4. Middleware
- Request logging
- API key authentication
- Global exception handling
- Response time tracking
5. Configuration
- appsettings.json configuration
- Environment-specific settings
- Options pattern with IOptions
6. Modern Features
- Route groups for API versioning
- Endpoint filters for validation
- Rate limiting
- Output caching
7. RESTful Best Practices
- Proper HTTP verbs
- Appropriate status codes
- HATEOAS links
- API versioning
Implementation Guide
Step 1: Create Project
dotnet new web -n AspNetCorePlayground
cd AspNetCorePlayground
dotnet add package FluentValidation.AspNetCore
dotnet add package Swashbuckle.AspNetCore
Step 2: Configure Services
var builder = WebApplication.CreateBuilder(args);
// Add services
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// DI registrations
builder.Services.AddSingleton<ICacheService, CacheService>();
builder.Services.AddScoped<IProductService, ProductService>();
builder.Services.AddTransient<IValidator<Product>, ProductValidator>();
// Rate limiting
builder.Services.AddRateLimiter(/* config */);
// Output caching
builder.Services.AddOutputCache();
var app = builder.Build();
Step 3: Configure Middleware Pipeline
// Exception handling
app.UseMiddleware<ExceptionHandlingMiddleware>();
// HTTPS
app.UseHttpsRedirection();
// Swagger
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
// Custom middleware
app.UseRequestLogging();
app.UseApiKey();
// Standard pipeline
app.UseRouting();
app.UseRateLimiter();
app.UseOutputCache();
// Map endpoints
app.MapControllers();
ProductEndpoints.Map(app);
CategoryEndpoints.Map(app);
app.Run();
Step 4: Implement Endpoints
Create static classes for endpoint mapping:
public static class ProductEndpoints
{
public static void Map(WebApplication app)
{
var products = app.MapGroup("/api/v1/products")
.WithTags("Products")
.WithOpenApi();
products.MapGet("/", GetAll);
products.MapGet("/{id:int}", GetById);
products.MapPost("/", Create);
products.MapPut("/{id:int}", Update);
products.MapDelete("/{id:int}", Delete);
}
private static async Task<IResult> GetAll(
IProductService service,
int page = 1,
int pageSize = 10)
{
var products = await service.GetAllAsync(page, pageSize);
return Results.Ok(products);
}
// Other handlers...
}
Deliverables
- Working API with both Minimal APIs and Controllers
- README.md explaining:
- Architecture decisions
- How to run the project
- API endpoints documentation
- Design patterns used
- Example requests in a
requests.httpfile - Comprehensive logging demonstrating middleware pipeline
- API documentation via Swagger
Testing Checklist
- All CRUD operations work
- Validation prevents invalid data
- Rate limiting blocks excessive requests
- Output caching improves performance
- Middleware logs all requests
- API key authentication works
- Exception handling catches errors
- Swagger UI is accessible
- Different service lifetimes demonstrated
- Both Minimal APIs and Controllers work
Bonus Features
- Add health check endpoints
- Implement API versioning (v1, v2)
- Add response compression
- Implement CORS policies
- Add structured logging with Serilog
Interview Preparation
Be ready to explain:
- Why you chose specific service lifetimes
- How the middleware pipeline is ordered
- When to use Minimal APIs vs Controllers
- How endpoint filters differ from middleware
- Your validation strategy