本节内容:
简介
OData 在上的定义是:"一个开放的协议, 允许创建和使用可查询、可互操作的 RESTful api 的简单的标准方式"。你可以在 ABP 里使用 OData,Abp.web.Api.OData 的 nuget 包简化了它的使用方式。
安装
安装 Nuget 包
在我们的 WebApi 项目里,先安装 Abp.Web.api.Odata 的 nuget 包:
- Install - Package Abp.Web.Api.OData
设置模块依赖
在我们的模块上设置对 AbpWebApiOdataModule 的依赖,例如:
- [DependsOn(typeof(AbpWebApiODataModule))] public class MyProjectWebApiModule: AbpModule {...
- }
查看更好地理解模块依赖。
配置你的实体
OData 需要声明哪个实体作为它的资源,我们应当在我们模块的方法里指定,如下所示:
- [DependsOn(typeof(AbpWebApiODataModule))] public class MyProjectWebApiModule: AbpModule {
- public override void PreInitialize() {
- var builder = Configuration.Modules.AbpWebApiOData().ODataModelBuilder;
- //Configure your entities here...
- builder.EntitySet("Persons");
- }...
- }
此处,我们 ODataModelBuilder 的引用,并给它设置了 Person 实体,类似地,你可以使用 EntitySet 来添加其它实体,查看获取更多信息。
创建控制器
Abp.Web.Api.OData 的 nuget 包包括了 AbpODataEntityController 基类(它扩展了标准的 ODataController),用它可更容易地创建你自己的控制器,如下是一个为 Person 实体创建一个 OData 端点的例子:
- public class PersonsController: AbpODataEntityController < Person > {
- public PersonsController(IRepository < Person > repository) : base(repository) {}
- }
这很简单,AbpODataEntityController 的所有方法都是 virtual,也就是说你可以重写 Get、Post、Put、Patch、Delete 和其它 Action 来添加自己的逻辑。
示例
这里我们列几个请求上面定义的控制器的基本的例子,假设应用工作在 http://localhost:61842 上,因为 OData 是一个标准的协议,你可以很容易地在网页上找到更深入的例子。
获取实体列表
获取所有 person。
请求
- GET http: //localhost:61842/odata/Persons
响应
- {
- "@odata.context": "http://localhost:61842/odata/$metadata#Persons",
- "value": [{
- "Name": "Douglas Adams",
- "IsDeleted": false,
- "DeleterUserId": null,
- "DeletionTime": null,
- "LastModificationTime": null,
- "LastModifierUserId": null,
- "CreationTime": "2015-11-07T20:12:39.363+03:00",
- "CreatorUserId": null,
- "Id": 1
- },
- {
- "Name": "John Nash",
- "IsDeleted": false,
- "DeleterUserId": null,
- "DeletionTime": null,
- "LastModificationTime": null,
- "LastModifierUserId": null,
- "CreationTime": "2015-11-07T20:12:39.363+03:00",
- "CreatorUserId": null,
- "Id": 2
- }]
- }
获取单个实体
获取 Id=2 的 person。
请求
- GET http: //localhost:61842/odata/Persons(2)
响应
- {
- "@odata.context": "http://localhost:61842/odata/$metadata#Persons/$entity",
- "Name": "John Nash",
- "IsDeleted": false,
- "DeleterUserId": null,
- "DeletionTime": null,
- "LastModificationTime": null,
- "LastModifierUserId": null,
- "CreationTime": "2015-11-07T20:12:39.363+03:00",
- "CreatorUserId": null,
- "Id": 2
- }
获取单个实体及导航属性
获取 Id=1 的 person 包含它的电话号码。
请求
- GET http: //localhost:61842/odata/Persons(1)?$expand=Phones
响应
- {
- "@odata.context": "http://localhost:61842/odata/$metadata#Persons/$entity",
- "Name": "Douglas Adams",
- "IsDeleted": false,
- "DeleterUserId": null,
- "DeletionTime": null,
- "LastModificationTime": null,
- "LastModifierUserId": null,
- "CreationTime": "2015-11-07T20:12:39.363+03:00",
- "CreatorUserId": null,
- "Id": 1,
- "Phones": [{
- "PersonId": 1,
- "Type": "Mobile",
- "Number": "4242424242",
- "CreationTime": "2015-11-07T20:12:39.363+03:00",
- "CreatorUserId": null,
- "Id": 1
- },
- {
- "PersonId": 1,
- "Type": "Mobile",
- "Number": "2424242424",
- "CreationTime": "2015-11-07T20:12:39.363+03:00",
- "CreatorUserId": null,
- "Id": 2
- }]
- }
查询
这里列举一个稍微复杂点的查询,包含过滤,排序和获取最前面 2 条结果。
请求
- GET http: //localhost:61842/odata/Persons?$filter=Name eq 'Douglas Adams'&$orderby=CreationTime&$top=2
响应
- {
- "@odata.context": "http://localhost:61842/odata/$metadata#Persons",
- "value": [{
- "Name": "Douglas Adams",
- "IsDeleted": false,
- "DeleterUserId": null,
- "DeletionTime": null,
- "LastModificationTime": null,
- "LastModifierUserId": null,
- "CreationTime": "2015-11-07T20:12:39.363+03:00",
- "CreatorUserId": null,
- "Id": 1
- },
- {
- "Name": "Douglas Adams",
- "IsDeleted": false,
- "DeleterUserId": null,
- "DeletionTime": null,
- "LastModificationTime": null,
- "LastModifierUserId": null,
- "CreationTime": "2016-01-12T20:29:03+02:00",
- "CreatorUserId": null,
- "Id": 3
- }]
- }
OData 支持分页,排序,过滤,投射等更多,请查阅。
创建一个新实体
接下来的例子,我们创建一个新 person。
请求
- POST http: //localhost:61842/odata/Persons
- {
- Name: "Galileo Galilei"
- }
此处,"Content-Type" 头是 "application/json"。
响应
- {
- "@odata.context": "http://localhost:61842/odata/$metadata#Persons/$entity",
- "Name": "Galileo Galilei",
- "IsDeleted": false,
- "DeleterUserId": null,
- "DeletionTime": null,
- "LastModificationTime": null,
- "LastModifierUserId": null,
- "CreationTime": "2016-01-12T20:36:04.1628263+02:00",
- "CreatorUserId": null,
- "Id": 4
- }
如果我们再次获取列表,我们可以看到这个新 person,也 OData 支持更新或删除一个已经存在的实体。
获取元数据
我们可以获取实体的元数据,如接下来的例子所示。
请求
- GET http: //localhost:61842/odata/$metadata
响应
- "1.0"encoding = "utf-8" ? >"4.0"xmlns: edmx = "http://docs.oasis-open.org/odata/ns/edmx" > "AbpODataDemo.People"xmlns = "http://docs.oasis-open.org/odata/ns/edm" > "Person" > "Id" / >
- "Name"Type = "Edm.String"Nullable = "false" / >"IsDeleted"Type = "Edm.Boolean"Nullable = "false" / >"DeleterUserId"Type = "Edm.Int64" / >"DeletionTime"Type = "Edm.DateTimeOffset" / >"LastModificationTime"Type = "Edm.DateTimeOffset" / >"LastModifierUserId"Type = "Edm.Int64" / >"CreationTime"Type = "Edm.DateTimeOffset"Nullable = "false" / >"CreatorUserId"Type = "Edm.Int64" / >"Id"Type = "Edm.Int32"Nullable = "false" / >"Phones"Type = "Collection(AbpODataDemo.People.Phone)" / >
- "Phone" > "Id" / >
- "PersonId"Type = "Edm.Int32" / >"Type"Type = "AbpODataDemo.People.PhoneType"Nullable = "false" / >"Number"Type = "Edm.String"Nullable = "false" / >"CreationTime"Type = "Edm.DateTimeOffset"Nullable = "false" / >"CreatorUserId"Type = "Edm.Int64" / >"Id"Type = "Edm.Int32"Nullable = "false" / >"Person"Type = "AbpODataDemo.People.Person" > "PersonId"ReferencedProperty = "Id" / >
- "PhoneType" > "Unknown"Value = "0" / >"Mobile"Value = "1" / >"Home"Value = "2" / >"Office"Value = "3" / >
- "Default"xmlns = "http://docs.oasis-open.org/odata/ns/edm" > "Container" > "Persons"EntityType = "AbpODataDemo.People.Person" / >
元数据用来查看服务信息。
示例项目
你可以从上获取这个示例项目的源代码。
来源: http://www.cnblogs.com/kid1412/p/6012938.html