The contact Manager web API is an Asp.net web API example program that demonstrates how to publish contact information through ASP. NET web API and allows you to add and delete contacts. The example address is http://code.msdn.microsoft.com/contact-manager-web-api-0e8e373d. Contact Manager web API example [1] The CRUD operation has been introduced. This article mainly introduces web API routing.
If you are familiar with ASP. net mvc, you will find that the web API routing (Routing) is very similar to MVC. The main difference is that the web API uses HTTP method (get, post, put, delete...) instead of using the URI path to select action. You can also use MVC-style routing in Web APIs. The following discussion focuses on Web APIs without the foundation of ASP. MVC.
Routing tables,Route table
In ASP. NET web API,ControllerIsClass (Class)To process HTTP requests (requests ). AllPublic Method(Public methods)Both are calledActionMethodOr abbreviationAction. When the web API framework receives a request, it routes the request to an action.
The framework uses a route table to determine which action will be called. If you use Web APIs in ASP. NET, the route table is defined in the Global. asax file. By default, Visual Studio is used to add a web API project. The project sample will create a default route for you:
Config. routes. maphttproute (
"Default", // route name
"{Controller}/{ID}/{ext}", // URL with Parameters
New {id = routeparameter. Optional, ext = routeparameter. optional} // parameter defaults
);
Note: If you use the self-host web API, you mustHttpselfhostconfigurationObjectDirectly set the route table.
Each object (entry) containsRouting Template(Route template). The default routing template for Web APIs is "API/{controller}/{ID}". In this template, "API" is a text path segment,{Controller}And{ID}Yes positioning Parameter.
When the web API framework receives an HTTP request, it tries to compare the URI to one of the routing templates of the route table. If there is no matching route, the client will receive a 404 error. For example, the following URI will match the default route:
·/API/contacts
·/API/contacts/1
When a conforming route is found, the web API selects controller and action:
· Search for controller. The web API adds "controller" to {controller} variable values.
· Search for action, view the HTTP method through Web APIs, and then check which action starts with the HTTP method name. For exampleGetRequestThe web API will check that the action is"Get... ", Such as" getcontact "or" getallcontacts ".This Convention is used inGet, post, put, deleteMethod. You can also use attributes in the Controller to enable other HTTP methods.
· Other positioning variables in the routing template, such as {ID}, correspond to the action parameter.
There are some possible HTTP requests:
Web API routing and HTTP Method |
HTTP Method |
UriPath |
Action |
Parameters |
Get |
/API/contacts |
Getallcontacts |
(None) |
Get |
/API/contacts/5 |
Getcontactbyid |
5 |
Delete |
/API/contacts/5 |
Deletecontact |
5 |
Post |
/API/contacts |
(None) |
|
Pay attention to{ID}Section. If it exists, it corresponds to the ID parameter in the action. In the preceding example, controller defines two get methods, oneYesId ParameterNoneId parameter. In addition, the POST request will fail because it is not defined in the Controller"Post... "Method.
Route parameters, routing variations
Next we will discuss route parameters.
HTTPMethod, HTTP methods
The web API uses the HTTP MethodNaming Convention(Naming Convention). You can also specifyHttpget, httppost, httpput, httpdeleteAttribute.
For example, the following get method corresponds to the GET request:
[Httpget]
Public httpresponsemessage <contact> get (int id)
{
......
}
Route of action name
In the default routing template, Web APIs use the HTTP method to select action. In any case, you can create a route with a URI containing the action name.
Config. routes. maphttproute (
"Default", // route name
"/API/{controller}/{action}/{ID}", // URL with Parameters
New {id = routeparameter. optional} // parameter defaults
);
This routing template. The action method in the Controller corresponds to the {action} parameter. Use this routing style,You must explicitly specify the allowed attribute inHTTPMethod. For example, there is a details method in the Controller:
[Httpget]
Public httpresponsemessage <contact> details (int id)
{
}
One"/API/contacts/details/1"GetRequestWill correspondDetailsMethod. This routing style is very similar to ASP. net mvc, or is close to an RPC-style API. In a restful API, you should avoid using verbs in Uri, because URI should define resources rather than actions.
Non-action
To prevent an action from being called, you can useNonactionAttribute. This is a signal that the Framework says this method is not an action method.
[Nonaction]
Public contact getprivatecontact (int id)
{
......
}
References
- Routing in ASP. NET web API
- Attribute Based Routing in ASP. NET web API
- Domain Based Routing with ASP. NET web API
- Magical web API action selector-http-verb and action name dispatching in a single Controller