RAM
RAM

Reputation: 2769

How to map root endpoint to an ASP.NET Core controller?

I'm designing a REST API with ASP.NET Core and I'd like to handle all requests sent to a root endpoint by a dedicated controller. Due to how to service implementing the API will be deployed, requests to the root endpoint will be routed to the server's root endpoint (i.e., https://localhost/)

AFAICT, Microsoft's tutorials on how to create web apis only cover the usecase where the API has no root endpoint, and all resources are neatly represented by a single controller which shares the same name as the route. In this scenario, routing is handled in a developer-friendly way by annotating the controller class with [Route("api/[controller]")], which creates a route sharing the controller class name. However, root endpoints don't have a name, thus it misses the critical design requirement to comply this strategy.

So far I've been using a class named HomeController which is decorated with [Route("")], which I'm not convinced is the best design approach.

So, does anyone know what's the best strategy to map an API's root endpoint to a controller in a ASP.NET Core project?

Upvotes: 4

Views: 5014

Answers (1)

dunwan
dunwan

Reputation: 1607

What worked for me was setting [Route("/")] on the endpoint as the controller is set to [Route("api/[controller]")]

Upvotes: 5

Related Questions