2017-5-29 Study record--WEBAPI (1)

Source: Internet
Author: User

I used to think that the Web server's API was implemented using ASHX or ASP. NET MVC to return Jsonresult.

When I first contacted WCF, some classmates told me at present more popular Webapi and websocket, so I also worry about how I always learn how to use the technology yo.

Use WEBAPI for the first time today

Specific steps:

1. First, I created an ASP. NET Project

2. Controllers and models folders are created in the root directory

3. Create warehousing (Storages.cs), people (Person.cs), students (Student), teacher (Teacher) classes under the Models folder and simulate the Student data as follows:

  

public static classstorages {public static ienumerable<student> Students {get; set; } public static ienumerable<teacher> Teachers {get; set; } static storages () {Students = new List<student>  () {New  Stu Dent () {id=1 , age=18 , Gender=false , name= "Gxq" }, New  Student () {id=2 , age=18  , Gender=false , name= "Gxq2" }, New  Student () {id=3 , age=18 , Gender=false , name= " Gxq3 "}, New  Student () {id=4 , age=18 , Gender=false , name= ' Gxq4 ' }}; Teachers = new List<teacher>  ();}} public class  Person {public int Id {GET, set ,} public string Name {get, set ;} public int Age {get; Set ,} public bool Gender {get; set ,}} public class  Student:person {} public class  Teacher : Person {}                

4. Create StudentsController.cs (Student) and TeachersController.cs (teacher) controllers under the Controllers folder

 public class  Studentscontroller:apicontroller {public ienumerable<student>  Get ()        {return  storages.students; Public Student Get (string  Item) {return Storages.Students.FirstOrDefault (U = u.name        = =  Item); public void  Post (Student entity) {ilist<student> list = storages.students as Ilist<student>
         
          ; Entity. Id = list. Max (s = = s.id) + 1 
          ; list. ADD (entity); } public void Put ([fromuri]string  item, [Frombody] Student entity) {Delete (item); Post (entity); public void Delete ([fromuri]string  Item) {var entity =  Getadmin (item); ilist<student> list = storages.students as ilist<student> ; list. Remove (entity); }} public class  Teacherscontroller:apicontroller {}       
           /span>     

5. New Global.asax file Configuration Webapi Routing

1 public     class    {3  4         protected void Application_Start (object                      GlobalConfiguration.Configuration.Routes.MapHttpRoute (7                 "Default_api", 8 "{Controller}/{item}" {11 item = }14}     

6, now completed the Webapi crud, but seems to have encountered some problems:

1, I do not know how to judge how to call the specific method, through the request and the method name corresponding matching?

2, I change the routing rule to {Controller}/{action}/{item} after changing the Get method name to admin after the call admin it prompts me not to provide a method of get, but I changed get to Getadmin, Display input getadmin can find the corresponding method, must be with a get identity?

3. Other issues are being studied in the study ... The above learning code has reference to the Intelligence podcast. NET Video

2017-5-29 Study record--WEBAPI (1)

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.