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

时间: 2024-08-10 00:06:40

AngularJS使用OData请求ASP.NET Web API资源的思路的相关文章

前端使用AngularJS的$resource,后端ASP.NET Web API,实现增删改查

AngularJS中的$resource服务相比$http服务更适合与RESTful服务进行交互.本篇后端使用ASP.NET Web API, 前端使用$resource,实现增删改查. 领域和上下文 首先领域先行. public class StudentVm { [Key] public int Id { get; set; } public string Name { get; set; } public string Age { get; set; } } 上下文. public cla

有关AngularJS请求Web API资源的思路

页面部分大致如下: <body ng-app="productManagement"> ... <div ng-include="'app/products/productListView.html'"></div> ... </body> productManagement是页面module的名称.页面内容通过ng-include加载productListView.html这个页面.注意:ng-include属性值是

对一个前端AngularJS,后端OData,ASP.NET Web API案例的理解

依然chsakell,他写了一篇前端AngularJS,后端OData,ASP.NET Web API的Demo,关于OData在ASP.NET Web API中的正删改查没有什么特别之处,但在前端调用API时,把各种调用使用$resouce封装在一个服务中的写法颇有借鉴意义. 文章:http://chsakell.com/2015/04/04/asp-net-web-api-feat-odata/源码:https://github.com/chsakell/odatawebapi 首先是领域模

Asp.Net Web API 2 官网菜鸟学习系列导航[持续更新中]

前言 本来一直参见于微软官网进行学习的, 官网网址http://www.asp.net/web-api.出于自己想锻炼一下学习阅读英文文章的目的,又可以学习下微软新发布的技术,其实也很久了,但自己菜鸟一枚,对自己来说都是新技术了.鉴于以上两个原因,本人打算借助google翻译和有道词典,来翻译学习这个系列,并通过博客园来记录自己的翻译学习过程.由于自己阅读水平的确太菜,在借助工具的情况下,有时候搞出来的也是蹩脚的语句,自己读着都难受,尤其是到了Web API路由的那两篇,所以自己想着是不是有别人

ASP.NET Web API基于OData的增删改查,以及处理实体间关系

本篇体验实现ASP.NET Web API基于OData的增删改查,以及处理实体间的关系. 首先是比较典型的一对多关系,Supplier和Product. public class Product { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } public string Category { get; set; } [ForeignKey("Sup

购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(3)--Idetity,OWIN前后端验证

原文:购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(3)--Idetity,OWIN前后端验证 chsakell分享了前端使用AngularJS,后端使用ASP.NET Web API的购物车案例,非常精彩,这里这里记录下对此项目的理解. 文章:http://chsakell.com/2015/01/31/angularjs-feat-web-api/http://chsakell.com/2015/03/07/angularjs-feat-web-api-

购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(2)--前端,以及前后端Session

原文:购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(2)--前端,以及前后端Session chsakell分享了前端使用AngularJS,后端使用ASP.NET Web API的购物车案例,非常精彩,这里这里记录下对此项目的理解. 文章:http://chsakell.com/2015/01/31/angularjs-feat-web-api/http://chsakell.com/2015/03/07/angularjs-feat-web-api-en

对一个前端使用AngularJS后端使用ASP.NET Web API项目的理解(3)

chsakell分享了一个前端使用AngularJS,后端使用ASP.NET Web API的项目. 源码: https://github.com/chsakell/spa-webapi-angularjs文章:http://chsakell.com/2015/08/23/building-single-page-applications-using-web-api-and-angularjs-free-e-book/ 这里记录下对此项目的理解.分为如下几篇: ● 对一个前端使用AngularJ

对一个前端使用AngularJS后端使用ASP.NET Web API项目的理解(2)

chsakell分享了一个前端使用AngularJS,后端使用ASP.NET Web API的项目. 源码: https://github.com/chsakell/spa-webapi-angularjs文章:http://chsakell.com/2015/08/23/building-single-page-applications-using-web-api-and-angularjs-free-e-book/ 这里记录下对此项目的理解.分为如下几篇: ● 对一个前端使用AngularJ