The advent of ASP. MVC4 Webapi has been there for a while. Recently because of doing some of their own fun, to do an API, just can learn this webapi.
WEBAPI Project Creation I'll take a look at Webapi's routing configuration.
Config. Routes.maphttproute ( name: "Defaultapi", routetemplate: "Api/{controller}/{id}", defaults:new {id = Routeparameter.optional} );
Direct API and controller controllers name it, which makes people wonder how this configuration can access the controller's method? We all know that the controller can add HttpGet or HttpPost to the method. So we have to add these HttpMethod to the method.
Let's look at the mapping between controller and URL.
Url |
Controller's action |
HttpMethod |
/api/controllername/id |
GetUser (int id) |
HttpGet |
/api/controllername/?name=xx |
GetUser (string name) |
HttpGet |
/api/controllername/ |
AddUser (Usermodel user) |
HttpPost |
/api/controllername/id |
DeleteUser (int id) |
Httpdelete |
/api/controllername/ |
DeleteAll () |
Httpdelete |
/api/controllername/ |
Putuser () |
Httpput |
Note: getuser (int id) The ID of this method can not be changed, after the change will not be able to access this method. The API configuration like this is simple, but it takes us to each method in the controller with HttpMethod instructions and the method in a controller does not write much. But don't worry, we can define our own routing configuration, as follows
Config. Routes.maphttproute ( name: "Defaultapi", routetemplate: "Api/{controller}/{action}/{id}", defaults: New {id = routeparameter.optional} );
This configuration allows us to use it flexibly.
The above is my personal study of some of the initial shallow understanding, welcome to point out my shortcomings.
ASP. NET MVC Webapi First Contact