• AngularJS使用OData请求ASP.NET Web API资源的思路


    本篇整理AngularJS使用OData请求ASP.NET Web API资源的思路。

    首先给ASP.NET Web API插上OData的翅膀,通过NuGet安装OData.

    然后,给controller中需要使用OData的Action加上EnableQuery特性,并让Action方法返回IQueryable<T>类型。

    public class ProductsController : ApiController
    {
        // GET: api/Products
        [EnableQuery()]
        public IQueryable<Product> Get()
        {
            var productRepository = new ProductRepository();
            return productRepository.Retrieve().AsQueryable();
        }
        
        ...
    }

    以上,EnableQuery特性还可以做出很多配置,比如:

    [EnableQuery(PageSize=50)]
    [EnableQuery(AllowedQueryOptons=AllowedQueryOptions.Skip | AllowedQueryOptions.Top)]


    在前端,可能先要自定义一个module,把uri路径作为这个module的一个常量。

    (function () {
        "use strict";
    
        angular.module("custommodule", ["ngResource"])
            .constant("appSettings", {
                serverPath: "http://localhost:49705/"
            });
    }());

    ngResource这个module来自于angular-resource.js文件,需要引用。

    接着可能要通过factory的方式,为custommodule这个module添加一个自定义service。

    function () {
        "use strict";
    
        //通过工厂的方式创建了一个服务
        angular.module("custommodule")
            .factory("productResource", ["$resource", "appSettings", productResouce]);
    
        function productResouce($resource, appSettings) {
            return $resource(appSettings.serverPath + "/api/products/:search");
        }
    }());

    以上,productResource这个service返回一个完整的api请求路径。

    接着需要把productResource这个service注入到某个controller中去。这时候就可以把OData的语法放在一个object对象传递给服务端。

    (function () {
        "use strict";
        angular
            .module("productManagement")
            .controller("ProductListCtrl",
                         ProductListCtrl);
    
        function ProductListCtrl(productResource) {
            var vm = this;
    
            vm.searchCriteria = "GDN";
    
            productResource.query({ $orderby: "Price desc" }, function (data) {
                vm.products = data;
            });
        }
    }());

    最终呈现出的OData相关uri大致是这样:http://localhost:52293/api/products?$filter=Price+gt+5&$orderby=Price

    基本就是这个思路

    有关OData的Operation:

    $top
    $skip
    $orderby
    $filter
    $select


    $filter的一些操作符:

    $filter: "Price eq 10"
    $filter: "Price ne 10"
    $filter: "Price gt 10"
    $filter: "Price ge 10" 大于等于
    $filter: "Price lt 10"
    $filter: "Price le 10" 小于或等于
    $filter: "Price gt 10 and Price lt 20"
    $filter: "Price eq 10 or Price eq 20"
    $filter: "(Price lt 10) and not (Price eq 0)"


    $filter的一些函数:

    $filter: "Starswith(ProductCode,'GDN')"
    $filter: "endswith(ProductCode, '001')"
    $filter: "contains(ProductCode, 'GDN')"
    $filter: "tolower(ProductCode) eq '001a'"
    $filter: "toupper(ProductCode) eq '001B'"

    $filter其它:

    运算符:Additon, subtraction
    日期:year, month, day, now, etc
    运算:round, floor, etc
    位置:distance, length, etc
    Lamda:any, all
    可空:null, etc

  • 相关阅读:
    C++ 与 C 的预处理能力
    unicore32linuxgcc 预定义宏
    内核中的原子上下文
    ConText
    PREEMPT_ACTIVE
    对象和类
    java的getClass()函数
    堆栈以及对象的引用
    public、protected、default、private作用域
    android 环境变量搭建
  • 原文地址:https://www.cnblogs.com/darrenji/p/4919504.html
Copyright © 2020-2023  润新知