In the 5-minute video at the following link, at the 1:10 mark, Jon Galloway says that adding a method called DeleteComment to his CommentsController controller class will by convention map automatically to the delete http verb.
How does MVC with WebApi know how to rout the methods to the right verbs? I know the routing in the global.asax.cs file routes requests to the correct controller, but how does a delete request get "mapped by convention" to the delete method, or any method? Especially when there can be more than 1 method for each verb? "By convention" makes me think it's just looking at the first word in a method name ... but if so, it would have to read the signature of the methods to tell two delete methods or two get methods apart ... and where is all this defined?
Video: http://www.asp.net/web-api/videos/getting-started/delete-and-update
Thanks!
Edit: Here is the code in the sample ValuesController class that comes in the WebApi template. This was the source of my original question. How does the "convention" that differentiates between these (and any other methods in the controller) work?
// GET /api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET /api/values/5
public string Get(int id)
{
return value;
}
This comes up quite often. And there are different views on that. I personally have not subscribed to any particular idea for now but it seems the one with one-controller-per-resource to be the most popular among the REST community.
So basically you can:
action
similar to MVC) but this is generally not meant to be used.I apologize in advance, this post strays a bit from what you asked, but all of this bubbled up when I read your question.
WebAPI Matching Semantic
The matching semantic used by (the default routes in) WebAPI is fairly simple.
So in your code sample a GET request without a parameter matches the
Get*( )
function without an parameters. A Get containing and ID looks for aGet***(int id)
.Examples
While the matching semantic is simple, it creates some confusion for MVC developers (well at least this developer). Lets look at some examples :
Odd Names - Your get method can be named anything, so long as it starts with "get". So in the case of a widget controller you can name your functions
GetStrawberry()
and it will still be matched. Think of the matching as something like :methodname.StartsWith("Get")
Multiple Matching Methods - What happens if you have two Get methods with no parameters?
GetStrawberry()
andGetOrange()
. As best I can tell, the function defined first (top of the file) in your code wins ...strange. This has the side effect of making some methods in your controller unreachable (at least with the default routes)....stranger.What to do?
Well, WebAPI is new and consensus is still coalescing. The community seems to be reaching for REST principles quite a bit. Yet, not every API can or should be RESTful, some are more naturally expressed in an RPC style. REST & what people call REST seems to be the source of quite a bit of confusion, well at least to Roy Fielding.
As a pragmatist, i suspect that many API's will be 70% RESTful, with a smattering of RPC style methods. First, the the controller proliferation alone (given the webapi binding method) is going to drive developers bonkers. Second, WebAPI doesn't really have a built in way to create a nested structure of api paths (meaning:
/api/controller/
is easy, but/api/CATEGORY/Sub-Category/Controller
is doable, but a pain).From my perspective, I would love to see the webAPI folder structure control the default API paths... meaning if I create a Category folder in my UI project then
/api/Category
would be the default path (something parallel to this MVC article).What did I do?
So, I had a few requirements: (1) to be able to use restful syntax in most case, (2) have some "namespace" separation of controllers (think sub-folders), (3) be able to call additional rpc-like methods when necessary. Implementing these requirements came down to clever routing.
Namespace
data token, to restrict what classes are searched for a particular route. This corresponds nicely to the typical namespace setup as you add folders to a UI project.Said Another Way
My solution came down to down to separating controllers a bit more so
/api/XXXX
didn't get too crowded.Category1
), and put api controllers within the folder.Widget1
in theCategory1
folder gets a default namespace ofUI.Category1.Widget1
./api/Category1/Widget
). The first mapping you see above accomplishes that, by hard coding/api/Category1
into the route, then thenamespace
token restricts classes that will be searched for a matching controller.