Reputation: 16284
I am using the latest Asp.Net Core 3 template in Visual Studio.
There is the default HomeController
, which is mapped via /Home
, and also its Index
action is reached via /
.
I added two Api controllers: DefaultController
and MController
, child class of ControllerBase
. These are mapped via /api/default
and /api/m
automatically. I can't find where the routing is configured.
What must I do to achieve the following routes:
/ -> /HomeController/Index (no change)
/home -> /HomeController (no change)
/api -> /api/default
/api/m -> /api/m (no change)
Upvotes: 0
Views: 437
Reputation: 3769
The default template will have the Route Annotation referencing the controllers name i.e.
[Route("api/[controller]")]
Try changing it to:
[Route("api")]
class DefaultController
{
....
}
Upvotes: 1
Reputation: 25380
Adding an extra [Route("api")]
will make it.
For example:
[ApiController] [Route("api", Name="DefaultAPI")] [Route("api/[controller]")] public class DefaultController : Controller { ... public IActionResult Index() { return Ok("abc"); } ... }
The following request will be routed to DefaultController/Index
GET https://localhost:5001/api
GET https://localhost:5001/api/Default
Upvotes: 0
Reputation: 20162
You can edit your launchSettings.json to do that
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/value", // here
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
or Right click your web project -> Select Properties -> Select the Debug tab on the left -> Then edit the 'Launch Url' field to set your own default launch url.
Upvotes: 0