By default, ASP.NET Web API selects an action method based on the HTTP verb and the action method parameters. In the second case, you have two action methods that can handle GET and they have one parameter each. When Web API tries to find an action method (more info here) for your GET, it will find both the methods.
You can follow RPC-style URI if you must have action methods like these. Add a mapping in WebApiConfig.cs like this.
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "RpcStyle",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
Then, make GET to URIs with action methods like so: http://localhost:port/api/usage/getmilk?a=1&b=2
.
BTW, your action methods will not be able to bind the query string to a complex type by default. Use FromUri
attribute like this: public int GetMilk([FromUri]MilkVM vm)
. GET requests must not have a request body and hence complex types will not be bound by default.