Advertisement

Controllers vs Minimal APIs interview questions and answers

15 Controllers vs Minimal APIs interview questions for ASP.NET Core roles, each with a model answer, examples and likely follow-ups.

15 questions
Advertisement

15 questions

Q11 Advanced Save

How would you migrate an existing controller-based API to minimal APIs incrementally without a risky big-bang rewrite?

A safe incremental approach: start by migrating simple, low-risk, low-traffic endpoints (like health checks or read-only reference data endpoints) first to build team familiarity, extract shared cross-cutting logic (validation, error handling) into reusable endpoint filters mirroring existing controller filter behavior, migrate remaining endpoints feature-by-feature (grouping related endpoints together to migrate as a cohesive unit rather than piecemeal), and maintain both styles coexisting in the same application throughout the transition, only removing the old controllers once each area's minimal API replacement has been validated in production.
// Phase 1: migrate simple endpoints first
app.MapGet("/health", () => Results.Ok());  // low-risk, migrated first

// Phase 2 (later): migrate a full feature area together
app.MapProductEndpoints();  // entire Products feature migrated as a cohesive unit

// Old ProductsController removed only after MapProductEndpoints is validated in production
Real-world example
A team migrates their 80-endpoint controller-based API to minimal APIs over three months, starting with five low-risk read-only endpoints, then tackling one full feature area (Products, then Orders, then Customers) at a time, keeping both styles running side-by-side throughout.
Likely follow-ups
  • What metrics would you track to validate each migrated feature area before removing the old controller?
  • How do you handle shared cross-cutting concerns (like a custom filter) that need to work identically in both styles during the transition?
CI/CD Publishing & Deployment Testing ASP.NET Core Applications
Q12 Intermediate Save

How does route-to-code discoverability differ between controllers' attribute-based routing and minimal APIs' explicit registration, and what are the trade-offs for large codebases?

Controllers' attribute-based routing ([HttpGet("products/{id}")] on a method) co-locates the route definition directly with its implementation, making it easy to find by opening the relevant controller file, but harder to get a bird's-eye view of an entire API's route surface without scanning every controller. Minimal APIs' explicit registration in Program.cs (or grouped extension methods) provides better at-a-glance visibility of the full route table in one place, but requires navigating away from the handler implementation itself (if defined elsewhere) to see its route.
// Controllers: route and implementation co-located, but scattered across many files
[HttpGet("{id}")]
public IActionResult Get(int id) => Ok(_service.GetById(id));  // route right here

// Minimal APIs: centralized route table, implementation possibly in a separate file
app.MapGet("/products/{id}", ProductHandlers.GetById);  // route here, logic elsewhere
Real-world example
A new team member onboarding to a minimal-API-based project quickly understands the full route surface by skimming a handful of MapXEndpoints() extension method files, compared to a controller-based sibling project requiring them to open dozens of individual controller files to build the same mental map.
Likely follow-ups
  • How do tools like Swagger/OpenAPI mitigate this discoverability difference for API consumers regardless of implementation style?
  • What organizational conventions maximize discoverability for each approach?
API Documentation with Swagger/OpenAPI Endpoint Metadata Route Constraints & Templates
Q13 Advanced Save

How do you write integration tests that work consistently for both controller-based and minimal API endpoints using WebApplicationFactory?

WebApplicationFactory<T> spins up an in-memory test server hosting the full application (regardless of whether its endpoints are controllers or minimal APIs, since both ultimately register into the same endpoint routing system) and issues real HTTP requests via an HttpClient -- from the test's perspective, there's no difference in how you write tests against either style, since you're testing observable HTTP behavior (status codes, response bodies) rather than the internal implementation mechanism.
public class ProductApiTests : IClassFixture<WebApplicationFactory<Program>> {
    private readonly HttpClient _client;
    public ProductApiTests(WebApplicationFactory<Program> factory) => _client = factory.CreateClient();

    [Fact]
    public async Task GetProduct_ReturnsOk() {
        var response = await _client.GetAsync("/api/products/1");  // works identically whether this is a controller action or minimal API endpoint
        response.EnsureSuccessStatusCode();
    }
}
Real-world example
A test suite covering both legacy controller-based endpoints and newly-migrated minimal API endpoints uses the exact same WebApplicationFactory-based testing pattern for both, requiring zero test code changes as endpoints are incrementally migrated from one style to the other.
Likely follow-ups
  • Does this testing approach reveal any behavioral differences between the two styles at all?
  • How would you unit test (not integration test) a minimal API handler function in isolation?
Testing ASP.NET Core Applications RESTful Web APIs & Controllers
Q14 Beginner Save

What is ControllerBase, and why do API controllers typically inherit from it rather than the full Controller class?

ControllerBase provides the core functionality API controllers need -- action results (Ok, NotFound, BadRequest), model binding, and the request/response context -- without the additional view-rendering support (View(), ViewBag, ViewData) that the full Controller class adds specifically for MVC applications returning HTML views, making ControllerBase the leaner, more appropriate base class for pure JSON/API-focused controllers.
// API controller: no view rendering needed
public class ProductsController : ControllerBase {
    [HttpGet]
    public IActionResult Get() => Ok(_products);
}

// MVC controller: needs view rendering support
public class HomeController : Controller {
    public IActionResult Index() => View();  // View() only available on Controller, not ControllerBase
}
Real-world example
A team building a pure JSON API consistently inherits from ControllerBase across every controller, since none of them ever need to render an HTML view, keeping the controllers' available surface area appropriately minimal.
Likely follow-ups
  • What specific members does Controller add on top of ControllerBase?
  • Can a single application mix controllers inheriting from each base class?
Controllers vs Minimal APIs RESTful Web APIs & Controllers
Q15 Intermediate Save

How do action results like Ok(), NotFound(), and BadRequest() in controllers compare to their minimal API equivalents like Results.Ok() and TypedResults?

Controllers use IActionResult-returning helper methods (Ok(), NotFound(), BadRequest()) inherited from ControllerBase, while minimal APIs use the static Results class (Results.Ok(), Results.NotFound()) or the newer, more strongly-typed TypedResults class (introduced for better OpenAPI metadata generation and compile-time type safety) -- both express equivalent HTTP semantics, just through different APIs suited to each style's conventions.
// Controller
public IActionResult GetProduct(int id) {
    var product = _service.GetById(id);
    return product is null ? NotFound() : Ok(product);
}

// Minimal API with TypedResults (better OpenAPI metadata)
app.MapGet("/products/{id}", Results<Ok<Product>, NotFound> (int id, IProductService service) => {
    var product = service.GetById(id);
    return product is null ? TypedResults.NotFound() : TypedResults.Ok(product);
});
Real-world example
A minimal API adopts TypedResults over the plain Results class specifically because its strongly-typed return signature lets OpenAPI tooling automatically infer and document every possible response type (200 and 404) without needing separate ProducesResponseType annotations.
Likely follow-ups
  • Why does TypedResults improve OpenAPI documentation generation compared to plain IResult?
  • Is there a meaningful runtime performance difference between IActionResult and IResult?
API Documentation with Swagger/OpenAPI Content Negotiation & Output Formatters

Showing 11–15 of 15