So, I am playing with the Web API (ASP.NET Core 2) and have problems with routing.
I have several controllers, such as:
SchoolController
TeacherController.
Both have gets: Get(int id)
The problem is that when I run it, I get an error at runtime, even when you actually call methods.
Attribute routes with the same name 'Get' must have the same template:
Action: MyProject.WebAPI.Controllers.SchoolController.Get (MyProject.WebAPI)' - Template: 'api/school/{id}'
Action: MyProject.WebAPI.Controllers.TeacherController.Get (MyProject.WebAPI)' - Template: 'api/teacher/{id}'
Why do this when controllers must have their own Gets, etc., so that you can:
/api/{controller}/1
etc... ?
Now I also have another Get method, both in their controllers and with a different method signature, as well as with a different name HttpGet ie:
[Produces("application/json")]
[Route("api/teacher")]
public class TeacherController : Controller
{
[HttpGet("{id}", Name = "Get")]
public IActionResult Get(int id)
{
}
}
And for the school controller:
[Produces("application/json")]
[Route("api/school")]
public class SchoolController : Controller
{
[HttpGet("{id}", Name = "Get")]
public IActionResult Get(int id)
{
}
[HttpGet("SearchBasic")]
public IActionResult SearchBasic(string schoolName, string zipCode)
{
}
}
To be clear, the question is:
Why am I getting runtime errors right after launching a web application?
The resulting files are on different controllers, so why are there conflicts?