Want to write a similar type, the code is not measured.
What is WEBAPI? My understanding is that webapi+jquery (front end) basically accomplishes the function of Web MVC, namely: So to understand, WEBAPI is equivalent to the background part of Web MVC.
The next straight example is that I have encountered or found some problems in the learning process.
First, create the WEBAPI project
(this link is not the focus of this chapter)
Second, the transmission parameters encountered problems
Background entity class (person):
1 namespace Webapi.models 2 {3 Public class person 4 {5 6 public int ID {get; set;} 7 public Strin G Name {get; set;} 8 public string Englishname {get; set;} 9 }10}
Background interface:
1 Public class Testcontroller:apicontroller 2 {3 publicly person getenglishname (int ID, string Name) 4 { 5 person mans = New Person (), 6 man.id = ID; 7 man . name = name; 8 Mans. Englishname = "Bert"; 9 return man;10 }11 }
Front-End AJAX requests:
1 <! DOCTYPE html> 2
Results:
As above, this is the most common one to visit WEBAPI Ajax request, Next we will talk about a few more special examples, hoping to strengthen the understanding and use of WEBAPI.
The ① code is modified as follows:
1 Public class Testcontroller:apicontroller 2 {3 publicly person setenglishname (int ID, string Name) 4 { 5 person mans = New Person (), 6 man.id = ID; 7 man . name = name; 8 Mans. Englishname = "Bert"; 9 return man;10 }11 }
1
Results:
Conclusion: Here we just changed the name of the interface, from Getenglishname to Setenglishname, why can't we find the method? The reason: Webapi for the background method interface without adding access to it (for example: [HttpPost]), and the method name is preceded by a GET, the default is a GET request. All in the example above, the method name does not indicate the request method or get start, it is naturally unable to find a way to allow access, it is recommended that no matter what type of request to set the access type on the method.
②post request to pass a parameter, data is not a key/value pair form, but data:{"" ":" Yzc "}, remember this special case, or the background is not get the front end to pass the past is worth, as the reason is: the Web API requires the [frombody] parameter of the request delivery, is to have a specific format that can be correctly obtained.
When ③post passes multiple parameters (the parameter marked [Frombody] can only occur once, the parameter marked as [Fromuri] may appear multiple times, and if the parameter marked as [Fromuri] is a simple parameter, the tag can be removed. )
When requested 1, data:JSON.stringify (x) and contenttype: "Application/json" are used together, or 2, data:{is a key-value pair}, and cannot be added contenttype: "Application /json ", both instances of the background [frombody] parameter can be obtained to the value. (After testing, when the intersection, such as: Data inside is a key value pair, and added contenttype: "Application/json", the background will not be able to access the data)
④ when the parameters of the POST request are many, it needs to be encapsulated in a class, so that the background also needs to create temporary classes to receive, and dynamic allows us to omit many classes. However, the configuration of the front-end Ajax request parameters is now only data:JSON.stringify (x) and contenttype: "Application/json" in the background to get the data.
Front-End Requests:
1 <! DOCTYPE html> 2
Background interface:
1 Public class Testcontroller:apicontroller 2 {3 [HttpPost] 4 public person getenglishname (dynamic Per) 5 {6 person man = new Person (), 7 man.id = per.id; 8 man . Name = per. Name; 9 Mans. Englishname = "Bert"; return man;11 }12 }
Results:
Problems with Ajax request delivery parameters