I'm writing a REST API and have the following problem:
With the resource /messages/, I have the following methods in my MessageController:
[HttpGet]
// retrieves all messages for a user
public HttpResponseMessage GetMessages(Guid RecordId) {}
[HttpGet]
// retrieves only a single messages, specified by guid
public HttpResponseMessage GetMessage(Guid RecordId) {}
In my WebApiConfig.cs I have the following routing:
config.Routes.MapHttpRoute(
name: "MessageRouting",
routeTemplate: "messages/{RecordId}/",
defaults: new { controller = "Message", RecordId= RouteParameter.Optional }
);
This, of course, fails because the routing doesn't know which method to call. The issue here is, to remain RESTful, I want to avoid having additional query parameters such as
GET /messages/?userid=1
Should I rather pull the userId from the AccessToken and call GetMessages as follows?
GET /messages
AccessToken=foo
I will run into the same problem using [HttpPost] and [HttpPut] as well - I always have either a collection, or a single item that I want to work with, so for each Http Method I will have at least 2 Methods to call.
I don't have the option of moving a GetMessages() method to the UserController, because there I will run into the same problem - it seems that with Web Api, a controller can only have each HTTP Method defined a single time, which makes it horribly hard to work with collections vs. single items.
I'm a little torn between remaining RESTful and having URLs uniquely identify resources, but it seems .NET doesn't give me a choice here, as I can't specify the HTTP Method in the routing. Or can I?