1. 程式人生 > >2017-5-29學習記錄——WebApi(1)

2017-5-29學習記錄——WebApi(1)

ora 必須 eache 配置 person span eve gen 流行

曾經我一直認為Web服務器的Api使用ashx或ASP.NET MVC中返回JsonResult來實現的。

當我第一次接觸WCF的時候,有同學告訴我目前比較流行WebApi和WebSocket了,於是我還擔心著我怎麽總在學不咋用了的技術喲。

今天第一次使用WebApi

具體步驟:

  1、首先我創建了一個ASP.NET的空項目 

  2、在根目錄創建了Controllers和Models文件夾

  3、在Models文件夾下創建倉儲(Storages.cs)、人(Person.cs)、學生(Student)、教師(Teacher)類並模擬了Student數據 如下:

  

  public static class Storages
    {
        public static IEnumerable<Student> Students { get; set; }
        public static IEnumerable<Teacher> Teachers { get; set; }
        static Storages()
        {
            Students = new List<Student>()
            {
                new Student(){
                    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、在Controllers文件夾下創建StudentsController.cs(學生)和TeachersController.cs(教師)控制器

   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
    {

    }

  5、新建Global.asax文件配置WebApi路由

 1     public class Global : System.Web.HttpApplication
 2     {
 3 
 4         protected void Application_Start(object sender, EventArgs e)
 5         {
 6              GlobalConfiguration.Configuration.Routes.MapHttpRoute(
 7                 "default_api",
 8                  "{controller}/{item}",
 9                  new
10                  {
11                      item = RouteParameter.Optional
12                  });
13         }
14     }

  6、現在就完成了WebApi的CRUD,但似乎遇到了些問題:

    1、我不知道怎樣去判斷調用那個具體的方法,通過請求方式與方法名對應的匹配嗎?

    2、我在路由規則改為{Controller}/{Action}/{Item}後將Get方法名改為Admin後顯示的調用Admin它提示我沒有提供Get的方法,但我將Get改為GetAdmin後、顯示的輸入GetAdmin即可找到對應方法,難道必須帶有Get標識嗎

    3、其他問題正在學習研究中...以上學習的代碼有參考傳智播客的.NET視頻

2017-5-29學習記錄——WebApi(1)