WEBAPI Learning Note The second response HttpGet message API

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.