(C#)WebApi,Get\Post\Put\Delete

webservice与webapi的区别:
webservice:用的soap协议;
webapi:用的http协议


webapi使用场景:
1、当你想创建一个基于HTTP的面向资源的服务并且可以使用HTTP的全部特征时(比如URIs、request/response头,缓存,版本控制,多种内容格式),你应该选择Web API
2、当你想让你的服务用于浏览器、手机、iPhone和平板电脑时,你应该选择Web API


webapi继承ApiController

 public class StudentsController:ApiController
    {
        /// <summary>
        /// Get 所有   /Students
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Student> Get()
        {
            return Storages.Students;
        }

可以在global文件中注册路由

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Security;
using System.Web.SessionState;

namespace WebApiTestMyself
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            GlobalConfiguration.Configuration.Routes.MapHttpRoute(
                "default_api",
                "{controller}/{item}",
                new {item =RouteParameter.Optional}
                );
        }
    }
}

demo文档结构
webapi


我本地没有数据,所以弄了个类似于EF上下文的类,初始化一些数据。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApiTestMyself.Models
{
    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,Name="狗尾草1",Age=23,Gender=false },
                new Student {Id=2,Name="狗尾草2",Age=26,Gender=true },
                new Student {Id=3,Name="狗尾草3",Age=29,Gender=false },
                new Student {Id=4,Name="狗尾草4",Age=43,Gender=true },
                new Student {Id=5,Name="狗尾草5",Age=13,Gender=false },
            };
            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
    {

    }
}

接口方法CURD

/// <summary>
        /// Get 所有   /Students
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Student> Get()
        {
            return Storages.Students;
        }
        /// <summary>
        /// Students/name
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Student Get(string name)
        {
            return Storages.Students.FirstOrDefault(s => s.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
        }
        /// <summary>
        /// post 添加
        /// </summary>
        /// <param name="entity"></param>
        public void Post(Student entity)
        {
            var list = Storages.Students as IList<Student>;
            entity.Id = Storages.Students.Max(s => s.Id) + 1;
            list.Add(entity);
        }
        /// <summary>
        /// 更改  Students/name
        /// </summary>
        /// <param name="name"></param>
        /// <param name="entity"></param>
        public void put([FromUri]string name,[FromBody]Student entity)
        {
            //var list = Storages.Students as IList<Student>;
            Delete(name);
            Post(entity);
        }
        /// <summary>
        /// 删除   Students/name
        /// </summary>
        /// <param name="name"></param>
        public void Delete([FromUri]string name)
        {
            var entity = Get(name);
            var list = Storages.Students as IList<Student>;
            list.Remove(entity);
        }

使用postman 测试,服务已通
postman测试接口

标签: webapi, mvc+webapi+ef, webservice

添加新评论