Currently, Web Api Controllers cannot host a mixture of verb-based action methods and traditional action name routing.
For example, consider the scenario of a "business entity" controller with CRUD actions (Get/Put/Post/Delete) that also needs some additional specific functionality such as listing relationships. Using a fictional CustomerController as an example:
GET /Api/Customer/1 - Get a customer entity by id
PUT /Api/Customer/1 - Update a customer entity
GET /Api/Customer/1/Orders - list a customer's orders
To handle this scenario, I can theoretically modify my default route:
routes.MapHttpRoute(
name: "DefaultIdAction",
routeTemplate: "{controller}/{id}/{action}",
defaults: new { id = RouteParameter.Optional, action = RouteParameter.Optional });
Everything works as expected for the traditional routing via "named" actions (ie/ action="Orders"), but fails for the verb-based action routing (when action = RouteParameter.Optional).
The verb-based routing finds multiple matching actions for GET:
public Customer Get(int id);
[HttpGet]
public IEnumerable<Order> Orders(int id);
It appears as though the verb-based action selection code is being too "greedy".
I propose the behavior of the verb-based action selection be changed to the following:
1) Find a list of candidate actions based on the verb and method signature, as today.
2) If no candidates are found, return 404.
3) If more than one candidate is found, filter the list of candidate actions and remove any action that is declarated with an "http verb attribute". In effect, in the case where there are multiple matches only the matches with the verb prefix in their name should be included. (ie/ only "Get" and "GetCustomer", not "Get", "GetCustomer", and "[HttpGet]ListProducts")
4) If the filtered list contains only one candidate, run the action. Otherwise, throw a multiple actions found error message.
A high level description for the change could be: "If more than one potential match for a verb-based action is found, only the actions that include the verb name as a prefix in their method name will be considered."
Comments: > Now that attribute routing is present in Web API Hmm, is it now available OOTB for the future release? What do u mean exactly by that?
For example, consider the scenario of a "business entity" controller with CRUD actions (Get/Put/Post/Delete) that also needs some additional specific functionality such as listing relationships. Using a fictional CustomerController as an example:
GET /Api/Customer/1 - Get a customer entity by id
PUT /Api/Customer/1 - Update a customer entity
GET /Api/Customer/1/Orders - list a customer's orders
To handle this scenario, I can theoretically modify my default route:
routes.MapHttpRoute(
name: "DefaultIdAction",
routeTemplate: "{controller}/{id}/{action}",
defaults: new { id = RouteParameter.Optional, action = RouteParameter.Optional });
Everything works as expected for the traditional routing via "named" actions (ie/ action="Orders"), but fails for the verb-based action routing (when action = RouteParameter.Optional).
The verb-based routing finds multiple matching actions for GET:
public Customer Get(int id);
[HttpGet]
public IEnumerable<Order> Orders(int id);
It appears as though the verb-based action selection code is being too "greedy".
I propose the behavior of the verb-based action selection be changed to the following:
1) Find a list of candidate actions based on the verb and method signature, as today.
2) If no candidates are found, return 404.
3) If more than one candidate is found, filter the list of candidate actions and remove any action that is declarated with an "http verb attribute". In effect, in the case where there are multiple matches only the matches with the verb prefix in their name should be included. (ie/ only "Get" and "GetCustomer", not "Get", "GetCustomer", and "[HttpGet]ListProducts")
4) If the filtered list contains only one candidate, run the action. Otherwise, throw a multiple actions found error message.
A high level description for the change could be: "If more than one potential match for a verb-based action is found, only the actions that include the verb name as a prefix in their method name will be considered."
Comments: > Now that attribute routing is present in Web API Hmm, is it now available OOTB for the future release? What do u mean exactly by that?