Reputation: 1351
I may have just been staring at this too long, or maybe I just misunderstand the idea behind WebAPI, but I'm looking to see if there is a way to make it so the routing table responds to CUSTOMIZED action names. For example, I want:
// -> /api/student/studentRecord?studentId=1
[HttpGet]
public Student StudentRecord(int studentId){
//Do Something and return the Student Record
}
// -> /api/student/newStudent?name=john
[HttpPost]
public int NewStudent(String name){
//Do whatever and return the new id
}
I'm not sure what I'm missing here, or if it can even be done. I've been scouring the internets for a while, and can't seem to figure it out.
Is the point of webAPI to just have a single PUT, POST, GET, etc in each controller, or can I do what I want it to do?
I've played around with the routing, but I think I made it worse! Every time I try to call something now, I get the same method being called.
This is what I have in the route config file:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Upvotes: 1
Views: 2309
Reputation: 4063
You don't even need the 'magical' action selector linked above (although it does sound quite cool) - WebApi allows to include the action
name (= controller method name, unless overriden) in the url.
So, in your example:
// -> /api/student/studentRecord?studentId=1
[HttpGet]
public Student StudentRecord(int studentId){}
the routing template would look like this:
routeTemplate: "api/{controller}/{action}"
Have a read through this to get more details: http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api
Upvotes: 2
Reputation: 13381
Have a look here Magical Web API action selector – HTTP-verb and action name dispatching in a single controller
You can have even more nicer API routes, for an example:
/api/student/1/studentrecord/2/
Upvotes: 2