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视频

posted @ 2017-05-29 20:39  Gxqsd  阅读(805)  评论(1编辑  收藏  举报