Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebAPI Core routing issues

Tags:

So, I am playing around with Web API (ASP.NET Core 2) and having routing issues.

I have several controllers such as:

SchoolController
TeacherController.

Both have Gets: Get(int id)

Problem is that when I run it, I get a runtime error before even actually being able to invoke the 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 would it do this when the controllers should have their own Gets etc... so you can do:

/api/{controller}/1  etc... ? 

Now, I also have another Get method, both in their controllers but with a different method signature along with a different HttpGet name i.e:

// TeachersController:  [Produces("application/json")] [Route("api/teacher")] public class TeacherController : Controller {      // GET: api/Teacher/5     [HttpGet("{id}", Name = "Get")]     public IActionResult Get(int id)     {          // BLAH     } } 

And for the school controller:

[Produces("application/json")] [Route("api/school")] public class SchoolController : Controller {      [HttpGet("{id}", Name = "Get")]     public IActionResult Get(int id)     {         // BLAH     }      [HttpGet("SearchBasic")]     public IActionResult SearchBasic(string schoolName, string zipCode)     {         // BLAH     } } 

To be clear - the question is:

  • Why do I get the runtime errors as soon as the web app is started?

  • The get's are on different controllers, so why would there be any conflicts?

like image 541
Ahmed ilyas Avatar asked Jan 29 '18 00:01

Ahmed ilyas


People also ask

Does .NET Web API provides routing support?

Web API 2 supports a new type of routing, called attribute routing. As the name implies, attribute routing uses attributes to define routes.

What is NET Core routing in Web API?

Routing in ASP.NET Core Web API application is the process of mapping the incoming HTTP Request (URL) to a particular resource i.e. controller action method. For the Routing Concept in ASP.NET Core Web API, we generally set some URLs for each resource.

How do I enable routing in .NET Core?

Inside the ConfigureRoute method, you can configure your routes; you can see that this method has to take a parameter of type IRouteBuilder. The goal of routing is to describe the rules that ASP.NET Core MVC will use to process an HTTP request and find a controller that can respond to that request.

How do I set the default route in .NET Core Web API?

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.


2 Answers

Controllers can't have actions with the same Route Name. They must be unique so that the route table can differentiate them.

Reference Routing to Controller Actions : Route Name

Route names can be used to generate a URL based on a specific route. Route names have no impact on the URL matching behavior of routing and are only used for URL generation. Route names must be unique application-wide.

emphasis mine

Update route names

[Route("api/teacher")] public class TeacherController : Controller {      // GET: api/Teacher/5     [HttpGet("{id}", Name = "GetTeacher")]     public IActionResult Get(int id) {         //...     } }  [Route("api/school")] public class SchoolController : Controller {     // GET: api/school/5     [HttpGet("{id}", Name = "GetSchool")]     public IActionResult Get(int id) {         //...     } } 
like image 128
Nkosi Avatar answered Oct 02 '22 18:10

Nkosi


Removing the name on get action from both controllers will solve the problem

like image 43
fmalh3iro Avatar answered Oct 02 '22 19:10

fmalh3iro