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)的统 ...
随机推荐
- idea如何编译maven项目
这里我们介绍两种方式,如何调试出窗口 点击菜单栏View->Tool Windows->Maven projects 点击菜单栏Help->Find Action(Ctrl+Shi ...
- 关于sklearn,监督学习几种模型的对比
# K近邻,适用于小型数据集,是很好的基准模型,容易解释 from sklearn.neighbors import KNeighborsClassifier # 线性模型,非常可靠的首选算法,适用于 ...
- jexus配置支持Owin
vi打开配置文件,加一行 OwinMain=xxx.dll ###################### # Web Site: Default ########################### ...
- CF601A 【The Two Routes】
看数据范围,然后果断邻接矩阵$Floyd$啊 对于公路和铁路,各建一个图,分别跑最短路,然后取最大值即可 #include<iostream> #include<cstdio> ...
- Java第三阶段学习(五、流的操作规律、Properties流、序列化流与反序列化流、打印流、commons-IO jar包)
一.流的操作规律 四个明确: 明确一:明确要操作的数据是数据源还是数据目的地 源:InputStream Reader 目的地:OutputStream Writer 先根据需求明确是要读还是写 ...
- word2vec 中的数学原理三 背景知识 语言模型
主要参考: word2vec 中的数学原理详解 自己动手写 word2vec
- 029 RDD Join相关API,以及程序
1.数据集 A表数据: 1 a 2 b 3 c B表数据: 1 aa1 1 aa2 2 bb1 2 bb2 2 bb3 4 dd1 2.join的分类 inner join left outer jo ...
- QT5 QSS
设计Qt程序Ui的时候,可以像WEB端使用CSS一样,使用QSS,使页面美化跟代码层分开,利于维护. 过程如下: 1.建立文本文件,写入样式表内容,更改文件后缀名为qss: 2.在工程中新建资源文件* ...
- win10下sublime text3 使用view in browser的快捷鍵添加方式
key-binding-user,于是,我在那里输入: [ { "keys": ["ctrl+b"], "command": " ...
- conda设置Python虚拟环境
conda设置Python虚拟环境 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/Co_zy/article/details/7741261 ...