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 }
);
You don’t even need the ‘magical’ action selector linked above (although it does sound quite cool) – WebApi allows to include the
actionname (= controller method name, unless overriden) in the url.So, in your example:
the routing template would look like this:
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