The route of the ASP. NET Web API is similar to ASP. NET MVC, which also puts the route in RouteTable. You can set up a route template in WebApiConfig.cs in the App_start folder. The default route template is:
Routes. Maphttproute (
Name: "API Default",
Routetemplate: "api/{controller}/{id}",
New {id = routeparameter.optional}
);
The static fragment API, which is used primarily to differentiate the routes of ASP.
Why isn't {action}? By default, an action can be found by convention, as long as the name of the action conforms to the Convention.
The placeholder variable {ID} maps the parameters of the action.
-Route according to convention
For Get,post,put,delete requests, if the name of the action begins with get, POST, PUT, delete, this is the Convention, meaning that you can route to the corresponding action without specifying the action in the requested URL.
Suppose you have one of these API controllers:
Public class Bookscontroller:apicontroller
{
Public void Getallbooks () {}
Public Ienumerable<book> Getbookbyid (int ID)
Public Httpresponsemessage deleteproduct (int id) {}
}
Browser input: Api/books and is a GET request
Without parameters, since Getallbooks's name begins with GET, it is customary to map to the Getallbooks method.
Browser input: API/BOOKS/8 and is a GET request
With parameters that are mapped to the Getbookbyid (int id) method. The Web API assigns a value of 8 of the string type to the parameter variable ID of type int.
Browser input: API/BOOKS/8 and is a delete request
Map to deleteproduct (int id)
Browser input: Api/books and is a POST request
There is no action corresponding to the POST request. Returns a 404 status code.
-Route based on HTTP method
HttpGet, Httpput, HttpPost, httpdelete attributes can be hit on the action.
Public class Bookscontroller:apicontroller
{
[HttpGet]
Public Book Findbook (ID) {}
}
If an action allows multiple HTTP methods, the Acceptverbs property is used.
Public class Bookscontroller; Apicontroller
{
[Acceptverbs ("GET", "HEAD")]
Public Book findproduct (ID) {}
}
-Consider the route of the action
We can set the template of the route in WebApiConfig.cs and take action into account.
Routes. Maphttproute (
Name: "actionapi",
Routetemplate: "api/{controller}/{action}/{id}",
New {id = routeparameter.optional}
);
In the API controller:
Public class Bookscontroller:apicontroller
{
[HttpGet]
Public string Details (int id);
}
In this case, a format similar to "API/BOOKS/DETAILS/8" is entered in the browser to map to the details (int id) method.
You can also use the ActionName property to alias an action:
Public class Bookscontroller:apicontroller
{
[ActionName ("Sth")]
Public Httpresposnemessage getsth (int id);
}
If you do not want the action to participate in the route, you can use the Noaction property.
Public class Bookscontroller:apicontroller
{
[Nonaction]
Public string Getsomedata () {}
}
Summary: The routing templates defined in WebApiConfig.cs are placed in the routetable. At the action level, if you want the request to be routed to the action, you can do so by using custom, HTTP method properties.
ASP. NET Web API Practice series 03, routing templates, routing conventions, routing settings