Carolyn Van Slyck

Mixing REST and RPC style methods on an ASP.NET Web API Controller

I wanted to have an API controller that had the normal RESTful actions (Get/Put/Post/Delete) and also some RPC style actions. This used to work in the MVC 4 Beta but now in RTM I would get the following error "Multiple actions were found that match the request: Get() on type CarController and Blue() on type CarController".

While I can understand that Microsoft is trying to encourage RESTful web services, it doesn't seem too farfetched to want simple filter methods on top of a normally RESTful controller. Since I didn't want to split my RPC style actions into a separate controller... a workaround had to be found! There is an open work item that may eventually make it back into the next release. In the meantime I found this StackOverflow answer and modified to get it working for the following routes:

  • /api/car (GET all cars)
  • /api/car/blue (GET all blue cars)
  • /api/car/15 (GET/POST/DELETE a specific car)

Here is a sample RouteConfig that will allow you to mix REST style actions with RPC style actions on the same controller.