zoukankan      html  css  js  c++  java
  • 如何理解 Web API

    • 什么是web API?
    • web API 控制器、路由
    • 测试  Web  API

     什么是web API ?

     简单说,API是接口,访问程序的某一个功能或者数据,实现移动端和客户端的程序之间的数据交互;web API,是可以通过HTTP的协议访问的web的上的API。

     如图1-1所示,发送请求,通过json的格式返回结果。

                                                                                                  图1-1

                                                           

     ASP.Net web API的特性:

    1. ASP.NET Web API是构建RESTful服务的理想平台。
    2. ASP.NET Web API构建于ASP.NET之上,支持ASP.NET请求/响应管道
    3. ASP.NET Web API将HTTP谓词映射到方法名称。
    4. ASP.NET Web API支持不同格式的响应数据。内置支持JSON,XML,BSON格式。
    5. ASP.NET Web API可以托管在IIS,自托管或支持.NET 4.0+的其他Web服务器中。
    6. ASP.NET Web API框架包括用于与Web API服务器通信的新HttpClient。HttpClient可用于ASP.MVC服务器端,Windows窗体应用程序,控制台应用程序或其他应用程序。

     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 = "获取数据成功";
                }
    
                return result;
    
            }

    详细说明:

            这是属性路由的获取数据的信息;

              ①  [RoutePrefix]: 整个控制器设置公共前缀

              ②  Route: 控制器中的路由

              ③ HttpGet:请求的方法

              ④ 路由url: [RoutePrefix]+【Route】是请求API的url地址

    HTTP方法:

    HttpPsot 、 HttpGet  、HttpDelete 、 HttpPut.

    测试Web API

    开发web API在本地进行测试,我们可以使用以下第三方工具来测试Web API或者在 web API页面上进行测试。

       Fildder

       posteman

  • 相关阅读:
    (转)C# BackgroundWorker组件的原理分析
    (转)App.Config详解及读写操作
    (转)C# 控制蜂鸣器发声
    存储过程小例子
    DB通用类:MySQL通用类
    poj 1936 All in All
    Bone Collector 01背包问题
    sdut1359 求最大和子矩阵
    求最大值
    后缀式求值
  • 原文地址:https://www.cnblogs.com/qy1234/p/11028162.html
Copyright © 2011-2022 走看看