I want to create a REStful web service.
This service returns "Meetings" for a certain
day, week or months.
I would create 3 Get methods which takes date values determined by the API user.
GetMeetingsByDay(currentDate)
GetMeetingsByWeek(startDate,endDate)
GetMeetingsByMonth(startDate,endDate)
Having 3 Get methods I could not access the API anymore just via http-method type 'GET' like:
// GET: api/meetings/date
because there will be multiple GET...
I could merge the ByWeek and ByMonth methods and do this instead:
GetMeetings(startDate,endDate);
How would you make this service RESTful?
I wouldn't think of it in terms of the method names - I'd think of it in terms of the URLs to start with:
/api/meetings?date=...
/api/meetings?startDate=...&endDate=...
Think of it as a collection of meetings, and the query parameters are just that - query parameters. I would expect any value after the meetings
element in the URL to be a meeting ID - for example, your GetMeetings
method might return a meeting with an ID of foobar
which I'd then expect to be able to fetch later with
/api/meetings/foobar
That suggests to me that you shouldn't have date
as part of the URL path at all.
In terms of implementation, I don't know enough about WebAPI routing to know whether you could implement that with two methods of:
[HttpGet]
[Route("api/meetings")]
public ... GetMeetings([FromUri] DateTime startDate, [FromUri] DateTime endDate)
[HttpGet]
[Route("api/meetings")]
public ... GetMeetings([FromUri] DateTime date)
... or whether you need to a single method with optional parameters:
[HttpGet]
[Route("api/meetings")]
public ... GetMeetings(
[FromUri] DateTime? date = null,
[FromUri] DateTime? startDate = null,
[FromUri] DateTime? endDate = null)
In the latter case you'd need to then validate that the set of arguments provided was valid.
(As noted in comments, you may not need the Route
here at all.)
See more on this question at Stackoverflow