如何理解 Web API
- 什么是web API?
- web API 控制器、路由
- 測試? Web? API
?
?什么是web API ?
?簡單說,API是接口,訪問程序的某一個功能或者數(shù)據(jù),實現(xiàn)移動端和客戶端的程序之間的數(shù)據(jù)交互;web API,是可以通過HTTP的協(xié)議訪問的web的上的API。
?如圖1-1所示,發(fā)送請求,通過json的格式返回結(jié)果。
?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 圖1-1
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
?ASP.Net web API的特性:
?
?Web API 控制器、路由
?
[RoutePrefix("data")] public class CNCheckerController : InitXpoController{[Route("GetAllOldEquipments")][HttpGet]public Result<List<AppOldEquipmentInfo>> GetAllOldEquipments(string Brand, string ModuleType, Palatfromtype PlatformTypes, int pageSize, int Page){Result<List<AppOldEquipmentInfo>> result = new Result<List<AppOldEquipmentInfo>>();result.Data = new List<AppOldEquipmentInfo>();using (Session){GroupOperator groupOperator = new GroupOperator(GroupOperatorType.And);if (!string.IsNullOrEmpty(Brand))groupOperator.Operands.Add(new BinaryOperator("FBrand", Brand));if (!string.IsNullOrEmpty(ModuleType))groupOperator.Operands.Add(new BinaryOperator("FModuleType", ModuleType));groupOperator.Operands.Add(CriteriaOperator.Parse("FPlatformTypes", PlatformTypes));groupOperator.Operands.Add(CriteriaOperator.Parse("ture"));var Searcholdequipment = new XPCollection<B_oldequipment>(Session, groupOperator, new SortProperty("createdate", DevExpress.Xpo.DB.SortingDirection.Descending)){ TopReturnedObjects = pageSize, SkipReturnedObjects = pageSize * Page };foreach (var oldequ in Searcholdequipment){AppOldEquipmentInfo appoldequipment = new AppOldEquipmentInfo();appoldequipment.Init(oldequ);result.Data.Add(appoldequipment);}result.Successed = true;result.Msg = "獲取數(shù)據(jù)成功";}return result;}?
詳細(xì)說明:
? ? ? ??這是屬性路由的獲取數(shù)據(jù)的信息;
? ? ? ? ? ①? [RoutePrefix]: 整個控制器設(shè)置公共前綴
? ? ? ? ? ②? Route: 控制器中的路由
? ? ? ? ? ③?HttpGet:請求的方法
? ? ? ? ? ④ 路由url:?[RoutePrefix]+【Route】是請求API的url地址
?
?
HTTP方法:
HttpPsot 、 HttpGet? 、HttpDelete 、 HttpPut.
?
測試Web API
開發(fā)web API在本地進(jìn)行測試,我們可以使用以下第三方工具來測試Web API或者在 web API頁面上進(jìn)行測試。
? ?Fildder
? ?posteman
?
轉(zhuǎn)載于:https://www.cnblogs.com/qy1234/p/11028162.html
總結(jié)
以上是生活随笔為你收集整理的如何理解 Web API的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php file_get_content
- 下一篇: 北风设计模式课程---深入理解[代理模式