10

I created a ASP.Net Core 2.0 with One controller, no problem. Then I added another Controller, then this Exception appears:

InvalidOperationException: The following errors occurred with attribute routing information:

Error 1: Attribute routes with the same name 'Get' must have the same template: Action: 'Patrimonio.Controllers.CategoriaController.Getcc (Patrimonio)' - Template: 'api/Categoria/{id}' Action: 'Patrimonio.Controllers.PatrimonioController.Getac (Patrimonio)' - Template: 'api/Patrimonio/{id}' Microsoft.AspNetCore.Mvc.Internal.ControllerActionDescriptorBuilder.Build(ApplicationModel application)

the first Controller has

 // GET: api/Categoria
 [Route("api/Categoria")]
 public class CategoriaController : Controller
 ...
 [HttpGet]
 public IEnumerable<string> Geta()
 {
     return new string[] { "value1", "value2" };
 }

the second has

 // GET: api/Patrimonio/5
 [Route("api/Patrimonio")]
 public class PatrimonioController : Controller
 ...
 [HttpGet("{id}", Name = "Get")]
 public string Getac(string id)
 {
     return "value" + id;
 }

Even with the Getac and Getcc, ASP.Net Core complains about same name 'Get' .

How to solve this?

2
  • 1
    Interesting ! This should work. do you have some other routing definition code you are using somewhere else ? Commented Oct 3, 2017 at 20:51
  • 1
    I tried app.UseMvc(); //app.UseMvcWithDefaultRoute(); but seems to have no effect to solve this. Commented Oct 3, 2017 at 20:58

1 Answer 1

28

Your error message does not correspond to the code you posted. But it appears that you have two [Http*(Name = "Get")] annotations in your program. Route names, however, must be unique.

https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing#route-name

Or more precisely, it appears from the error message that two actions with the same route name must have exactly the same URL template. The reason for this is that the route name is primarily used for reverse routing (i.e. generating a URL to an action), and if the name isn't unique then the URL is ambiguous - unless all routes with that name have the same template.

Try replacing

[HttpGet("{id}", Name = "Get")]

with

[HttpGet("{id}")]
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome. You diagnosed precisely. I did not perceive that the "Get" ASP.Net Core was complaining was on the Attribute declaration Name="Get". I tought it was the Get name of the method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.