Best Practices — Do's & Don'ts — .NET Core Web API
✅ Do's # Practice Reason 1 Use DTOs for all API responses Prevents exposing internal entities and over-posting 2 Use [Required], [StringLength], [Range] on DTOs Input validation at API boundary 3 Use parameterized queries in ADO.NET Prevents SQL injection 4 Use AsNoTracking() for read-only queries Better EF Core performance 5 Use primary constructors (C# 12) Cleaner DI, less boilerplate 6 Store secrets in environment variables Never commit to source control 7 Use HTTPS + HSTS in production Encrypt all traffic 8 Add security headers to every response X-Content-Type-Options, X-Frame-Options 9 Rate limit per IP address Block brute-force and abuse 10 Log structured data, not sensitive data Never log tokens, passwords, or PII 11 Use CancellationToken on all async methods Enables request…
Key Takeaways
Never expose database entities — use DTOs for all API responses.
Always use parameterized queries — never concatenate user input into SQL.
Store all secrets in environment variables, never in source control.
Rate limit, validate inputs, and add security headers to every response.
Test every layer independently with proper mocking.
Use async/await throughout — never .Result or .Wait().