Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 230
RAM Avatar asked Oct 30 '25 02:10

RAM


1 Answers

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

like image 129
dunwan Avatar answered Oct 31 '25 17:10

dunwan