This article mainly introduces the Get method in the Wepapi response HTTP method:
To create an empty WEBAPI solution:
In the Model folder, add the person class with the following content:
Public classPerson { Public stringID {Get;Set;} Public stringName {Get;Set; } Public stringSex {Get;Set; } Public stringPhone {Get;Set; } Public stringaddres {Get;Set; } }
Below we create an empty WEBAPI controller Peoplecontroller in the Controller folder with the following contents:
Public classPeoplecontroller:apicontroller {person [] people=Newperson[]{NewPerson () {ID="001", Name="Dongdong", Sex="male", Phone="123456789", Address="Henan Nanyang" }, NewPerson () {ID="002", Name="Wang", Sex="female", Phone="456123789", Address="Henan Nanyang" }, NewPerson () {ID="003", Name="Zhao", Sex="male", Phone="123789456", Address="Henan Nanyang" }, NewPerson () {ID="004", Name="Sun", Sex="female", Phone="789123456", Address="Henan Nanyang" }, }; PublicIenumerable<person>Get () {returnpeople; } PublicIhttpactionresult Getperson (stringID) {varperson = people. FirstOrDefault (p) = P.id = =ID); if(Person = =NULL) { returnNotFound (); } returnOk (person); } }
In the controller above, I added a person's array to replace the data, defined two get messages that start with get to respond to HTTP, the first get function returns all the person data, and the second Getperson function returns data for a specific ID.
Test on the advenced:
One
Url=http://localhost:7281/api/people
Results:
[4]0: {ID:"001"Name:"Dongdong"Sex:"male"Phone:"123456789"Address:"Henan Nanyang"}-1: {ID:"002"Name:"Wang"Sex:"female"Phone:"456123789"Address:"Henan Nanyang"}-2: {ID:"003"Name:"Zhao"Sex:"male"Phone:"123789456"Address:"Henan Nanyang"}-3: {ID:"004"Name:"Sun"Sex:"female"Phone:"789123456"Address:"Henan Nanyang"}
Two:
url=http://localhost:7281/api/people/001
Results:
" 001 " " dongdong " "Male" " 123456789 "" Henan Nanyang "}
Webapi: The function of the controller when responding to the Get method can be prefixed with get or by using C # 's attribute property, such as [Route ("/getall")], and so on.
Because the Get method's response is only to read the data, its parameters can be easily passed by URL, but how to pass parameters when the parameters of the response function of the Put,post method are complex parameters? And how to pass the advanced test?
WEBAPI Learning Note The second response HttpGet message API