liveBook · Manning

Routing conflict

As explained in Building Web APIs with ASP.NET Core

A routing conflict occurs when multiple handlers are configured for the same HTTP method and path combination.

A routing conflict occurs when multiple handlers use the same HTTP method and path. Controllers and Minimal API endpoints can coexist, but they should handle different routes. section 2.4.3

Middleware ordering determines which handler receives the request first. With app.MapGet("/error", ...) before app.MapControllers(), the Minimal API handler handles the request, leaving the controller implementation effectively unused. section 2.4.3

Swagger exposes the conflict

Defining both a controller action and a Minimal API endpoint for GET /error can make Swagger UI fail with HTTP 500. The Swagger generator reports a conflicting method/path combination because OpenAPI requires each combination to be unique. Resolve the problem by deleting the redundant handler or assigning it a different route. section 2.4.3

using Microsoft.AspNetCore.Mvc;
 
namespace MyBGList.Controllers
{
    [ApiController]
    public class ErrorController : ControllerBase
    {
        [Route("/error")]             #1
        [HttpGet]                     #2
        public IActionResult Error()
        {
            return Problem();         #3
        }
    }
}

Compare the controller-based /error handler with the Minimal API equivalent in b5 to identify duplicate routing configuration. section 2.4.3

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Traces duplicate routing through middleware order to Swagger’s unique method/path requirement and offers deletion or route changes.
Examples
Uses competing GET /error handlers, Swagger UI’s HTTP 500 failure, and the generated conflicting-actions exception.
What it assumes
Assumes familiarity with Program.cs middleware ordering, HTTP methods, paths, Controllers, and Minimal API mapping.
What you can do after
Diagnose duplicate method/path combinations and remove or reroute the redundant handler.
How it connects
Connects HTTP methods and paths with middleware ordering, Controllers, Minimal APIs, Swagger, OpenAPI, and route configuration.

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.