文檔目錄
本節內容:
簡介
OData在odata.org上的定義是:“一個開放的協議,允許創建和使用可查詢、可互操作的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需要聲明哪個實體作為它的資源,我們應當在我們模塊的PreInitialize方法裡指定,如下所示:
[DependsOn(typeof(AbpWebApiODataModule))] public class MyProjectWebApiModule : AbpModule { public override void PreInitialize() { var builder = Configuration.Modules.AbpWebApiOData().ODataModelBuilder; //Configure your entities here... builder.EntitySet<Person>("Persons"); } ... }
此處,我們ODataModelBuilder的引用,並給它設置了Person實體,類似地,你可以使用EntitySet來添加其它實體,查看OData文檔獲取更多信息。
創建控制器
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
響應
<?xml version="1.0" encoding="utf-8"?> <edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx"> <edmx:DataServices> <Schema Namespace="AbpODataDemo.People" xmlns="http://docs.oasis-open.org/odata/ns/edm"> <EntityType Name="Person"> <Key> <PropertyRef Name="Id" /> </Key> <Property Name="Name" Type="Edm.String" Nullable="false" /> <Property Name="IsDeleted" Type="Edm.Boolean" Nullable="false" /> <Property Name="DeleterUserId" Type="Edm.Int64" /> <Property Name="DeletionTime" Type="Edm.DateTimeOffset" /> <Property Name="LastModificationTime" Type="Edm.DateTimeOffset" /> <Property Name="LastModifierUserId" Type="Edm.Int64" /> <Property Name="CreationTime" Type="Edm.DateTimeOffset" Nullable="false" /> <Property Name="CreatorUserId" Type="Edm.Int64" /> <Property Name="Id" Type="Edm.Int32" Nullable="false" /> <NavigationProperty Name="Phones" Type="Collection(AbpODataDemo.People.Phone)" /> </EntityType> <EntityType Name="Phone"> <Key> <PropertyRef Name="Id" /> </Key> <Property Name="PersonId" Type="Edm.Int32" /> <Property Name="Type" Type="AbpODataDemo.People.PhoneType" Nullable="false" /> <Property Name="Number" Type="Edm.String" Nullable="false" /> <Property Name="CreationTime" Type="Edm.DateTimeOffset" Nullable="false" /> <Property Name="CreatorUserId" Type="Edm.Int64" /> <Property Name="Id" Type="Edm.Int32" Nullable="false" /> <NavigationProperty Name="Person" Type="AbpODataDemo.People.Person"> <ReferentialConstraint Property="PersonId" ReferencedProperty="Id" /> </NavigationProperty> </EntityType> <EnumType Name="PhoneType"> <Member Name="Unknown" Value="0" /> <Member Name="Mobile" Value="1" /> <Member Name="Home" Value="2" /> <Member Name="Office" Value="3" /> </EnumType> </Schema> <Schema Namespace="Default" xmlns="http://docs.oasis-open.org/odata/ns/edm"> <EntityContainer Name="Container"> <EntitySet Name="Persons" EntityType="AbpODataDemo.People.Person" /> </EntityContainer> </Schema> </edmx:DataServices> </edmx:Edmx>
元數據用來查看服務信息。
示例項目
你可以從https://github.com/aspnetboilerplate/sample-odata上獲取這個示例項目的源代碼。