Why does the REST API get a call going to the wrong method?

I have two methods getin my API controller that takes no arguments and one that takes an integer argument.

The path to the API page /api/contact. When I go here, the page displays as expected.

However, when I change the path to /api/contact/4to try to call a method getthat takes an integer argument, instead, the code simply calls the same get method without any arguments. I know this by introducing breakpoints and debugging. What is going wrong?

public PhoneInfo[] Get()
{
    return contactRepository.GetAllContacts();
}

public PhoneInfo[] Get(int phn)
{
    return contactRepository.GetMessages(phn.ToString());
}
+4
source share
1 answer

WebApi , , {vars} global.asax/routing .

global.asax :

RouteTable.Routes.MapHttpRoute(name: "DefaultApi",
                     routeTemplate: "api/{controller}/{id}",
                     defaults: new { id = System.Web.Http.RouteParameter.Optional }); 

, , id var, url api/contact/{id}, :

   public string Get(int id)
       return "test";
   }

id phn, WebApi .

RouteAttribute:

 [Route("api/contact/{phn}"), HttpGet]
 public string Get(int phn)
   return "another value";
 }
+3

All Articles