In Webapi the choice of action is mainly through: Action method name matching, HTTP method matching, parameter matching three steps.
http method Matching
WEBAPI provides the choice of three HTTP methods, namely: Method prefix, acceptverbs feature, httpxxx feature
Method prefix:
In the previous article, this approach was used to prefix the HTTP method as action. Like GetAll, Postbyurl.
In addition, you can specify only one HTTP method, such as Getputpostall () to support only get requests
Acceptverbs characteristics
The Acceptverbs feature provides two constructors:
public Acceptverbsattribute (params string[] methods)
public Acceptverbsattribute (string method)
With these two constructors we can specify the HTTP request mode for the action, for example:
[acceptverbs("POST","PUT")]
public IEnumerable<figure> Postbybody ([frombody] figure )
This time, you can use the Post,put way to access.
Httpxxx characteristics
The httpxxx approach is similar to Acceptverbs, and we can see it as a Acceptverbs anti-error method (which may be a mistake in spelling the HTTP method) in the following format:
[httpput]
[httppost]
public IEnumerable<figure> Postbybody ([frombody] figure )
The ASP. NET WEBAPI defines 7 different types of attributes:
HttpGet
Httphead
Httpput
HttpPost
Httppatch
Httpoptions
Httpdelete
If the Acceptverbs and Httpxxx methods are used, the prefix will be invalidated. Acceptverbsattribute and Httpxxxattribute both implement Interface Iactionhttpmethodprovider, so the final HTTP request method that can be used is the set of the two methods .
Action Method name match
Method name matching because it is relatively simple, here we look at the VS2013 default generated route template.
public static void Register (httpconfiguration config) { //Web API Configuration and service //Web API Route CONFIG. Maphttpattributeroutes (); Config. Routes.maphttproute ( name: "Defaultapi", routetemplate: "Api/{controller}/{id}", defaults:new {id = Routeparameter.optional} ); }
VS2013 By default
The routing format provided is "Api/{controller}/{id}" and does not provide an action, so the action lookup is only matched against the parameter in the way it is requested.
In figure, two methods are defined, namely:
public IEnumerable<figure> GetAll ()
Public figure getfigure (string firstName)
They are accessed using the following URLs:
Http://localhost:7075/api/Figure
Http://localhost:7075/api/Figure?firstName=Bran
At this point they call the method GetAll, getbyquerystring
In addition, a get method with no parameter and no return value is added to the Figurecontroller, and now, with http://localhost:7075/api/Figure access, an error message "Multiple operations found matching the request" is obtained.
Parameter matching
Methods in. NET can be overloaded, so there is also a matching process for the parameters in the action selection. The parameters are derived from Routedata and querystring. In the match, the following conditions occur (in order to be in):
1. No default value for all parameters
In this case, all parameters will be matched, as follows:
Public figure getfromquerystring (string firstName)
Public figure getfromquerystring (string firstName,string lastName)
Access with the following URL
Http://localhost:7075/api/Figure/GetByQueryString?firstName=Bran
Http://localhost:7075/api/Figure/GetByQueryString?firstName=Bran&lastName=Stack
The getfromquerystring (string firstName), getfromquerystring (String firstname,string lastName), are called separately.
However, we use http://localhost:7075/api/Figure/GetByQueryString to access it and return "404 Not FOUND".
2. There are default only parameters
The first thing you can confirm is that all parameters are prioritized to match, and if there is no matching action then two-round match
public IEnumerable< figure> Getfromquerystringdefaultvalue (string lastname= "Stack")
Public figure getfromquerystringdefaultvalue (string firstName, string lastname= "Stack")
Action one can be accessed through the following URLs
Http://localhost:7075/api/Figure/GetFromQueryStringDefaultValue
Http://localhost:7075/api/Figure/GetFromQueryStringDefaultValue?lastName=Jon
Action two can also be accessed by using the following action
Http://localhost:7075/api/Figure/GetFromQueryStringDefaultValue?firstName=Bran
Http://localhost:7075/api/Figure/GetFromQueryStringDefaultValue?firstName=Bran&lastName=Stack
If we set the default value for the FirstName of Action two, then the above four URLs are accessed and we get the "multiple actions found matching the request" error.
3. Parameter types
The overloads of a method are determined by the number of parameters, the type of arguments, and the order of the parameter types, because of the particularity of Web Access (matching parameters only by parameter names), so the order of parameter types is not used as a matching principle. Both of these cases match the number of parameters. In the Figurecontroller overloaded method Getfromquerystringtype
public string getfromquerystringtype (string x, string y)
public int getfromquerystringtype (int x, int y)
Access via http://localhost:7075/api/Figure/GetFromQueryStringType?x=1&y=2. At this point, you get "multiple actions found that match the request."
Therefore, it is not possible to overload only by the parameter type (the same as the parameter name).
A method that can act as an action
- Public instance methods
- Method is not inherited from Apicontroller
Source
Github:https://github.com/barlowdu/webapi (api_2)
The choice of ASP. WebAPI 02-action (i)