2

In my API project I have currently 1 controller with the following syntax:

Abstract base controller

[Produces("application/json")]
[Route("api/v{version:apiVersion}/[controller]")]
[ApiController]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status406NotAcceptable)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public abstract class BaseApiController : ControllerBase
{
     // ....
}

TestApiController

public class TestApiController : BaseApiController 
{
    // Parent action methods
    [HttpGet]
    
    [HttpPost]

    ...

    // Child action methods
    [HttpGet("{test:guid}/child")]
    
    [HttpGet("{test:guid}/child/{id:guid}")]

    ...
}

This gives us the following urls:

http://localhost/api/v1.0/test
http://localhost/api/v1.0/test/1
http://localhost/api/v1.0/test/1/child
http://localhost/api/v1.0/test/1/child/1

Question

How can I split the controller into multiple files without braking the urls but to have a cleaner overview.

So a controller for all the test actions and another controller for all the child actions.

2
  • 1
    I'm not sure I fully understand the question but you can simply use multiple partial classes. Commented Jul 2, 2021 at 18:53
  • While a partial class sounds nice, I'd keep the classes small by breaking it up to TestController for /test/id? and TestChildController for /test/id/? endpoints (and one for each subresource). This way the controller stays small and the list of dependencies stay manageable (unless you're using parameter injection via [FromServices], of course) Commented Jul 2, 2021 at 19:00

2 Answers 2

2

After some more searching on the internet I came across the following interesting Nuget package:

Nuget: Ardalis.ApiEndpoints.

Source code and examples about this can be found here:

Github: Ardalis.ApiEndpoints.

This allows us to separate controller into folders and for each action a separated class to give use exactly what we want.

Sign up to request clarification or add additional context in comments.

Comments

1

I'm not talking from an abundance of experience with controllers here (I have done some), but I think you'll find that the controller is the controller - in that there's no easy way to have a controller-route that isn't 1:1.

But, if you want to make controllers more manageable you could just keep them as lean as possible and put the bulk of code into classes that the controller calls.

Alternatively, rather than trying to split/decompose the controller and not the route, rework the overall API design/structure so that you have more controllers and routes. The Interface Segregation Principle comes to mind here (yes it's born out of OO but the principle is still applicable).

At the end of the day, you have calls coming into to API's defined by their routes, and off to the controllers - it doesn't really matter how that all looks (is structured) as long as the API is sufficiently sensible, intuitive and fit for purpose.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.