WebAPI例子
1.WebApiConfig配置API路由
using System; using System.Collections.Generic; using System.Linq; using System.Web.Http; namespace WebAPIDemo1 { public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Web API 配置和服务 // Web API 路由 config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", //api/controller控制器名/action方法名/id参数名 routeTemplate: "api/{controller}/{action}/{id}", // routeTemplate: "api/{controller}", defaults: new { id = RouteParameter.Optional } ); } } }
2.配置Global.asax文件中Application_Start
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Http; using System.Web.Routing; namespace WebAPIDemo1 { public class WebApiApplication : System.Web.HttpApplication { protected void Application_Start() { GlobalConfiguration.Configure(WebApiConfig.Register); } } }
3.创建PersonController控制器
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using WebAPIDemo1.Models; namespace WebAPIDemo1.Controllers { public class PersonController : ApiController { #region GET // /api/person/Get public string[] Get() { return new string[] { "ru", "qq", "wx" }; } // /api/person/Get?id= public string Get(int id) { return "这是:" + id; } // /api/person/Get?name= public string Get(string name) { return "姓名:" + name; } #endregion #region Post /// <summary> /// 参数在地址栏上 /// </summary> /// <param name="value"></param> /// <returns></returns> // /api/person/Post?value=123 public string Post(string value) { return "Post请求值" + value; } /// <summary> /// 参数不在地址栏上 /// </summary> /// <param name="value"></param> /// <returns>/api/person/Post </returns> public string Post([FromBody]BodyValue value) { return "Post请求值" + value.value; } #endregion public string Put(int id, [FromBody] string value) { return "Put请求值:" + value + ",id:" + id; } public string Delete(int id) { return "数据id为" + id + "删除成功"; } //[HttpGet] [HttpPost] // /api/person/Login?Uid=admin&Pwd=123 public bool Login(string Uid, string Pwd) { if (Uid == "admin" && Pwd == "123") return true; else return false; } [HttpGet] //入参实体参数FromUri //api/person/Login2?Uid=admin&Pwd=456 public bool Login2([FromUri] UseInfo Model) { if (Model.Uid == "admin" && Model.Pwd == "123") return true; else return false; } //Post 表单提交类型application/x-www-form-urlencoded //body==>raw==>json==>{"Uid":"admin","Pwd":"123"} //api/person/Login3 [HttpPost] public bool Login3([FromBody] UseInfo Model) { if (Model.Uid == "admin" && Model.Pwd == "123") return true; else return false; } } }
4.创建DogController
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; namespace WebAPIDemo1.Controllers { [RoutePrefix("api/Test")] //第三种/api/Dog/Get public class DogController : ApiController { [HttpGet] //[Route("api/Dog/Get888")]第二种/api/Dog/Get888 //[Route("Get888")] //第一种/api/Test/Get888 //不加route /api/Dog/Get888 public string Get() { return "Get666"; } [HttpGet] [Route("Login/{Uid}/{Pwd}")] //加route /api/Test/Login/123/456 //参数地址化 public bool Login(string Uid,string Pwd) { if (Uid == "admin" && Pwd == "123") return true; else return false; } } }