I have the Web API controller with 2 methods - let's say the first method returns the plain project list and the second one returns all projects assigned to the specific user.
public class ProjectController: ApiController
{
public IQueryable<Project> Get() { ... }
[HttpGet]
public IQueryable<Project> ForUser(int userId) { ... }
}
The method implementation is not important in this case.
Web API route config is also adjusted to support the custom method names.
config.Routes.MapHttpRoute(
"DefaultApi",
"api/v1/{controller}/{id}",
new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
"DefaultApiWithAction",
"api/v1/{controller}/{action}");
It works fine, I can access both /api/v1/projects/
and /api/v1/projects/forUser/
endpoints, but seems that the route engine is too smart, so it decides that /api/v1/projects?userId=1
request may match the ForUser(..)
method (due to the userId
argument name, I guess) and ignores the {action}
part of the route.
Is there any way to avoid this behavior and require the action part to be explicitly specified in the URL?