天天看點

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

曾經我一直認為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視訊

繼續閱讀