4.11 — Project Structure Conventions for a Web API¶
← Roadmap · Section: 4. Fundamentals and Application Startup Prev: 4.10 Static files · Next: 5.1 What middleware is
Markers: ⭐ · Confidence: 🔴 · Last reviewed: —
TL;DR¶
There is no official ASP.NET Core project structure. The framework does not care where your files live — but the choice shapes how the codebase ages.
- Start with one project and folders. Split into separate projects only when you have a real reason.
- Two common approaches: layer-based (Controllers, Services, Models) and feature-based (Orders, Payments, Customers).
- Feature-based scales better, because a change to one feature touches one folder instead of five.
Definitions¶
| Term | What it is | What it means |
|---|---|---|
| Project | one .csproj |
Compiles to one assembly. See 3.6. |
| Solution | one .sln file |
A container grouping several projects. |
| Layer-based structure | a folder convention | Folders named after technical roles: Controllers/, Services/, Models/. |
| Feature-based structure | a folder convention | Folders named after business capabilities: Orders/, Payments/. |
| Vertical slice | an architectural style | Each feature holds everything it needs, top to bottom. See 22.11. |
| Clean Architecture | an architectural style | Layers as separate projects, with dependencies pointing inward. See 22.7. |
| Modular monolith | an architectural style | One deployable app, internally split into independent modules. |
| DTO | a class | Data Transfer Object. The shape you expose over HTTP, distinct from your database entity. |
| Registration extension | a static method | An AddSomething(this IServiceCollection) method grouping a module's DI registrations. |
The concept¶
The framework has no opinion¶
This surprises people coming from frameworks with enforced conventions. ASP.NET Core does not
require Controllers/ to be called that, does not care where your services live, and will
happily find a controller anywhere in the assembly.
So structure is entirely a maintainability decision, and the honest answer to "what is the right structure?" is it depends on size.
The failure mode worth avoiding is starting with the structure you think a large system needs. A four-project Clean Architecture solution for a service with six endpoints is a lot of ceremony buying nothing — and it is a very common mistake, because it looks professional.
A picture: you do not build a warehouse with loading bays and a forklift to store six boxes. You use a cupboard, and you build the warehouse when you have enough boxes to justify it.
How it works¶
The simple version — layer-based¶
The default from the templates, and fine for small projects:
MyApp.Api/
├── Program.cs
├── appsettings.json
├── Controllers/
│ ├── OrdersController.cs
│ ├── CustomersController.cs
│ └── PaymentsController.cs
├── Services/
│ ├── OrderService.cs
│ ├── CustomerService.cs
│ └── PaymentService.cs
├── Models/
│ ├── Order.cs
│ ├── Customer.cs
│ └── Payment.cs
├── Data/
│ └── AppDbContext.cs
└── wwwroot/
Folders are named after what a thing is technically.
It works well up to maybe 10–15 endpoints. Past that, a problem appears: adding one field
to an order means editing Controllers/OrdersController.cs, Services/OrderService.cs,
Models/Order.cs and Data/AppDbContext.cs — four folders, and the related code is scattered.
Feature-based — the one that scales¶
Folders named after what the code does for the business:
MyApp.Api/
├── Program.cs
├── Features/
│ ├── Orders/
│ │ ├── OrdersController.cs
│ │ ├── OrderService.cs
│ │ ├── OrderRepository.cs
│ │ ├── CreateOrderRequest.cs
│ │ ├── OrderDto.cs
│ │ └── OrderModuleExtensions.cs ← its own DI registration
│ ├── Payments/
│ │ ├── PaymentsController.cs
│ │ └── ...
│ └── Customers/
│ └── ...
├── Common/
│ ├── Middleware/
│ └── Extensions/
└── Data/
└── AppDbContext.cs
Now everything about orders is in one folder. Three practical consequences:
- A change to orders touches one folder. Easier to review, easier to reason about.
- Deleting a feature is deleting a folder. With layers, orphaned pieces get left behind.
- Ownership is obvious. One team owns
Features/Payments/.
The trade-off is that shared code needs a home, which is what Common/ is for. Watch that it
does not become a dumping ground.
For a new API of any size, feature-based is the better default.
How it actually works — the registration extension pattern¶
Feature folders pair naturally with keeping each module's DI registrations next to its code:
// Features/Orders/OrderModuleExtensions.cs
public static class OrderModuleExtensions
{
public static IServiceCollection AddOrderModule(
this IServiceCollection services,
IConfiguration config)
{
services.AddScoped<IOrderRepository, EfOrderRepository>();
services.AddScoped<IOrderService, OrderService>();
services.Configure<OrderOptions>(config.GetSection("Orders"));
return services; // return it so calls chain
}
}
// Program.cs stays readable no matter how many features exist
builder.Services
.AddOrderModule(builder.Configuration)
.AddPaymentModule(builder.Configuration)
.AddCustomerModule(builder.Configuration);
Two benefits: Program.cs does not grow linearly with the codebase, and deleting a feature
folder does not leave orphaned registrations behind — because they were inside it.
The deeper detail — when to split into separate projects¶
The real question is not folders but projects. Each project is a separate assembly with its own build.
Reasons that justify a split:
- You genuinely share code between two deployables — an API and a worker service both needing the same domain types.
- You want to enforce a dependency direction. If
Domainis a separate project that references nothing, the compiler stops anyone importing EF Core into it. Folders cannot enforce that. - Separate test projects, which is standard and not really a debate.
Reasons that do not:
- "It looks more professional."
- "The tutorial did it."
- "We might need it later."
Every extra project adds build time, another .csproj to maintain, and more navigation
friction. Start with one, split when the pain is real.
A reasonable progression:
# Stage 1 — a small API
MyApp.Api/
MyApp.Api.Tests/
# Stage 2 — logic worth isolating and testing separately
MyApp.Api/
MyApp.Core/ ← domain + business logic, no framework dependencies
MyApp.Core.Tests/
MyApp.Api.Tests/
# Stage 3 — full Clean Architecture, if it earns its place
MyApp.Api/ ← controllers, middleware, DI wiring
MyApp.Application/ ← use cases, interfaces, DTOs
MyApp.Domain/ ← entities, value objects. References NOTHING.
MyApp.Infrastructure/ ← EF Core, HTTP clients, file system
Most services never need stage 3, and reaching it too early costs more than it saves. See 22.7.
Things that are not negotiable¶
A few conventions genuinely matter:
Keep DTOs separate from entities. Returning your EF entity from a controller leaks your database schema, invites over-posting, and causes serialization cycles through navigation properties. See 10.11 and 11.10.
Nothing sensitive in wwwroot. It is public — 4.10.
Controllers stay thin. A controller reads the request, calls one service, and returns a result. Business logic in a controller cannot be unit tested without the whole web stack.
// ✓ Thin — delegates and translates
[HttpPost]
public async Task<IActionResult> Create(CreateOrderRequest request, CancellationToken ct)
{
var result = await _orders.CreateAsync(request, ct);
return result.IsSuccess
? CreatedAtAction(nameof(Get), new { id = result.Value.Id }, result.Value)
: BadRequest(result.Error);
}
Code¶
A realistic feature-based API structure:
MyApp.Api/
├── Program.cs
├── appsettings.json
├── appsettings.Development.json
│
├── Features/
│ ├── Orders/
│ │ ├── OrdersController.cs
│ │ ├── OrderService.cs
│ │ ├── IOrderService.cs
│ │ ├── Contracts/
│ │ │ ├── CreateOrderRequest.cs
│ │ │ └── OrderResponse.cs
│ │ ├── Validators/
│ │ │ └── CreateOrderValidator.cs
│ │ └── OrderModuleExtensions.cs
│ │
│ └── Payments/
│ └── ... (same shape)
│
├── Common/
│ ├── Middleware/
│ │ └── GlobalExceptionHandler.cs
│ ├── Extensions/
│ │ └── ServiceCollectionExtensions.cs
│ └── Results/
│ └── Result.cs
│
├── Data/
│ ├── AppDbContext.cs
│ ├── Configurations/ ← IEntityTypeConfiguration classes
│ └── Migrations/
│
└── wwwroot/ ← public. Nothing sensitive.
// Program.cs — short regardless of how many features exist
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddDbContext<AppDbContext>(o =>
o.UseSqlServer(builder.Configuration.GetConnectionString("Default")));
builder.Services
.AddOrderModule(builder.Configuration)
.AddPaymentModule(builder.Configuration);
var app = builder.Build();
app.UseExceptionHandler();
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();
Notice that Program.cs looks the same whether there are three features or thirty. That is the
point of the pattern.
Interview questions¶
Q: How would you structure an ASP.NET Core Web API project?
The framework has no required structure, so it is purely a maintainability decision. For
anything beyond a handful of endpoints I would organise by feature rather than by technical
layer — a folder per business capability holding its controller, service, DTOs and validators
together, rather than separate Controllers, Services and Models folders. The reason is
that changes arrive by feature, so a feature folder means one place to look, and deleting a
capability is deleting a folder rather than hunting for orphaned pieces across five directories.
Q: When would you split into multiple projects?
When there is a concrete reason, not by default. Genuine reasons are sharing code between two deployables, such as an API and a worker service needing the same domain types, or enforcing a dependency direction — if the domain is its own project referencing nothing, the compiler prevents anyone importing EF Core into it, which folders cannot do. Test projects are separate as a matter of course. What I would avoid is starting with a four-project Clean Architecture solution for a small service, because every project adds build time and navigation friction and you get nothing back until the codebase is large enough to need the separation.
Q: What is the difference between layer-based and feature-based organisation?
Layer-based names folders after technical roles — Controllers, Services, Models — which is what the templates give you and works fine for small projects. Feature-based names them after business capabilities — Orders, Payments, Customers — with each folder holding everything that feature needs. The practical difference shows up as the codebase grows: with layers, adding a field to an order means editing four folders, and the related code is scattered. With features it is one folder. Feature-based also makes ownership obvious when several teams work in the same repository.
Q: What conventions genuinely matter regardless of structure?
Three. DTOs stay separate from entities, because returning an EF entity leaks your schema,
enables over-posting, and causes serialization problems through navigation properties. Nothing
sensitive goes in wwwroot, since it is served publicly with no authorization. And controllers
stay thin — read the request, call one service, translate the result — because business logic
in a controller cannot be unit tested without standing up the whole web stack.
Traps ⚠️¶
- Adopting Clean Architecture before the codebase needs it. Four projects for six endpoints is cost with no benefit.
Common/becoming a dumping ground. If everything depends on it, you have recreated a single layer.- Returning EF entities from controllers. Leaks the schema and invites over-posting.
- Business logic in controllers. Untestable without the full web stack.
- DI registrations all in
Program.cs. It grows without limit and deleting a feature leaves orphaned lines. - Anything sensitive in
wwwroot. It is public. - Splitting projects to "enforce" a rule that a folder would have expressed. Only a project boundary is compiler-enforced — use one when that enforcement is the point.
Related¶
- 4.3 Program.cs — the registration extension pattern
- 4.10 Static files — why
wwwrootis special - 1.14 Extension methods — how module registration works
- 10.11 DTOs and mapping — keeping contracts separate
- 22.7 Clean Architecture — when the split earns its place
- 22.11 Vertical slice architecture — feature-based taken further
- 3.6 Assemblies — what a project boundary actually is