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)的统 ...
随机推荐
- python 获取二进制文件
import requests response = requests.get('https://www.baidu.com/aladdin/img/tools/ip.png')with open(' ...
- 利用shell找出15分钟内修改的文件
如果你的文件只是生成不会修改的话,可以这样: find * -mmin -15 2>/dev/null 如果可能修改,则需要这样(先创建一个 15 分之前的时间戳的文件,然后找比这个文件新的文件 ...
- 【并行计算】用MPI进行分布式内存编程(一)
通过上一篇关于并行计算准备部分的介绍,我们知道MPI(Message-Passing-Interface 消息传递接口)实现并行是进程级别的,通过通信在进程之间进行消息传递.MPI并不是一种新的开发语 ...
- 【前端node.js框架】node.js框架express
server.js /* 以下代码等下会有详细的解释 */ var express = require('express'); // 用来引入express模块 var app = express() ...
- Java第三阶段学习(五、流的操作规律、Properties流、序列化流与反序列化流、打印流、commons-IO jar包)
一.流的操作规律 四个明确: 明确一:明确要操作的数据是数据源还是数据目的地 源:InputStream Reader 目的地:OutputStream Writer 先根据需求明确是要读还是写 ...
- 数学之美——HMM模型(一)介绍
一直想写点关于数学方面的blog,这对于数据挖掘分析,NLP处理等都有着比较重要的作用,之前在CSDN上想写点HMM方面的文章,一直没写成,最近几天终于抽点时间完成了HMM的文章,加以整理,遂有这个系 ...
- js上传插件uploadify自动检测不到flash控件的问题
[问题描述] 项目开发中,由于使用了js的一个上传插件uploadify,下载的是flash版本的,后来在谷歌浏览器上运行时经常报flash控件未安装,虽然下图是uploadify自动检测自动弹出来的 ...
- 【*】深入理解redis主从复制原理
1.复制过程 从节点执行 slaveof 命令. 从节点只是保存了 slaveof 命令中主节点的信息,并没有立即发起复制. 从节点内部的定时任务发现有主节点的信息,开始使用 socket 连接主节点 ...
- BZOJ2160: 拉拉队排练
Description 艾利斯顿商学院篮球队要参加一年一度的市篮球比赛了.拉拉队是篮球比赛的一个看点,好的拉拉队往往能帮助球队增加士气,赢得最终的比赛.所以作为拉拉队队长的楚雨荨同学知道,帮助篮球队训 ...
- pygame系列_小球完全弹性碰撞游戏_源码下载
之前做了一个基于python的tkinter的小球完全碰撞游戏: python开发_tkinter_小球完全弹性碰撞游戏_源码下载 今天利用业余时间,写了一个功能要强大一些的小球完全碰撞游戏: 游戏名 ...