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
AngularJS使用OData请求ASP.NET Web API资源的思路的更多相关文章
- 前端使用AngularJS的$resource,后端ASP.NET Web API,实现分页、过滤
在上一篇中实现了增删改查,本篇实现分页和过滤. 本系列包括: 1.前端使用AngularJS的$resource,后端ASP.NET Web API,实现增删改查2.前端使用AngularJS的$re ...
- 前端使用AngularJS的$resource,后端ASP.NET Web API,实现增删改查
AngularJS中的$resource服务相比$http服务更适合与RESTful服务进行交互.本篇后端使用ASP.NET Web API, 前端使用$resource,实现增删改查. 本系列包括: ...
- OData查询ASP.NET Web API全攻略
本篇使用ASP.NET Web API来体验OData各种query. 首先是本篇即将用到的Model.使用的OData版本是4.0. public class Customer { public i ...
- 有关AngularJS请求Web API资源的思路
页面部分大致如下: <body ng-app="productManagement"> ... <div ng-include="'app/produc ...
- 对一个前端AngularJS,后端OData,ASP.NET Web API案例的理解
依然chsakell,他写了一篇前端AngularJS,后端OData,ASP.NET Web API的Demo,关于OData在ASP.NET Web API中的正删改查没有什么特别之处,但在前端调 ...
- ASP.NET Web API 2 external logins with Facebook and Google in AngularJS app
转载:http://bitoftech.net/2014/08/11/asp-net-web-api-2-external-logins-social-logins-facebook-google-a ...
- [水煮 ASP.NET Web API 2 方法论] 目 录
一.ASP.NET 中的 Web API [水煮 ASP.NET Web API2 方法论](1-1)在MVC 应用程序中添加 ASP.NET Web API 与 ASP.NET MVC 在同一个进程 ...
- 杂项:ASP.NET Web API
ylbtech-杂项:ASP.NET Web API ASP.NET Web API 是一种框架,用于轻松构建可以访问多种客户端(包括浏览器和移动设备)的 HTTP 服务. ASP.NET Web A ...
- 使用ASP.NET Web API 2创建OData v4 终结点
开放数据协议(Open Data Protocol[简称OData])是用于Web的数据访问协议.OData提供了一种对数据集进行CRUD操作(Create,Read,Update,Delete)的统 ...
随机推荐
- 记录自己对EventLoop和性能问题处理的一点心得【转】
转自:http://www.cnblogs.com/lanyuliuyun/p/4483384.html 1.EventLoop 这里说的EventLoop不是指某一个具体的库或是框架,而是指一种程序 ...
- tar命令的实用详解(C参数和排除文件 --exclude)
一.tar:从压缩包中解压出指定文件 [root@d176 test]# tar ztf nrpe-2.12.tar.gz |grep srcnrpe-2.12/src/nrpe-2.12/src/. ...
- IntelliJ IDEA 中安装junit插件
1.在Intellij IDEA 中安装了 Junit,TestNG插件,首先检查一下intellij IDEA 是否在安装时默认安装了这两个插件,检查方法 打开 settings -->Plu ...
- stylus项目知识点
1.在项目中,引入.sty文件的时候,用来下面方式 @import "~common/stylus/variable.styl" ~ 是stylus的写法,参考https://gi ...
- OPENCV SVM介绍和自带例子
依据机器学习算法如何学习数据可分为3类:有监督学习:从有标签的数据学习,得到模型参数,对测试数据正确分类:无监督学习:没有标签,计算机自己寻找输入数据可能的模型:强化学习(reinforcement ...
- java 内部类使用 .this 和 .new
如果需要生成对外部类对象的引用,可以使用外部类的名字后面紧跟圆点和this,这样产生的引用自动地具有正确的类型,这一点在编译器就被知晓并受到检查,因此并没有运行时开销 //: innerclasses ...
- (转)Python函数式编程——map()、reduce()
转自:http://www.jianshu.com/p/7fe3408e6048 1.map(func,seq1[,seq2...]) Python 函数式编程中的map()函数是将func作用于se ...
- 跟据经纬度实现附近搜索Java实现
现在很多手机软件都用附近搜索功能,但具体是怎么实现的呢>在网上查了很多资料,mysql空间数据库.矩形算法.geohash我都用过了,当数据上了百万之后mysql空间数据库方法是最强最精确的(查 ...
- rabbitMQ的安装(Windows下)
在公司接触到这一块,信息中间件的使用,在公司没有时间了解的更加深入,只是在简单的使用,这里将深入学习一番. 参考:http://blog.csdn.net/lu1005287365/article/d ...
- 【Android Studio】为Android Studio设置HTTP代理
[Android Studio]为Android Studio设置HTTP代理 大陆的墙很厚很高,初次安装Android Studio下载SDK等必定失败,设置代理方法如下: 1. 到androi ...