Building Smart API Endpoints with C# Source Generators
Exposing CRUD endpoints for every model means writing the same boilerplate over and over. This post uses Roslyn source generators to auto-generate complete REST endpoints at compile-time — no runtime magic, no repetitive controllers — through a tool the author built called AutoApiGenerator.
What you’ll learn
- How the
[AutoApi]attribute drives generation, includingRoutePrefixandDbContextTypeoptions - The generator pipeline: discovering annotated entities, analyzing their syntax/semantic models, and emitting a
.gen.csbase plus a partial.csfor custom logic - Wiring in EF Core for async, DI-backed CRUD with ID-based
GET/{id},PUT, andDELETE - Extending or overriding generated behavior cleanly via partial classes
- A comparison of manual controllers, hand-coded minimal APIs, and the generator across boilerplate, customizability, type safety, and compile-time guarantees
The full source is on GitHub, so it’s a great read if you want to learn practical source-generator techniques you can adapt.