ASP.NET Core Web API Interview Question #81
How should nested resources be represented in API routes?
Routing, Versioning, OpenAPI/Swagger & API Documentation Mid-Level Intermediate
Quick Interview Answer
Nested routes can express a meaningful parent-child relationship, such as a customer's orders. They are useful when the child is naturally scoped by the parent.
Detailed Explanation
Nested routes can express a meaningful parent-child relationship, such as a customer's orders. They are useful when the child is naturally scoped by the parent.
Avoid excessive nesting because deeply hierarchical URLs become difficult to maintain. If a child resource has a globally meaningful identifier, a simpler direct resource route may be preferable.
Code Example
GET /api/customers/42/orders
GET /api/customers/42/orders/100
[HttpGet("customers/{customerId:int}/orders")]
public IActionResult GetOrders(int customerId) => Ok();