OData 带更新的实例,并能取得元数据格式类型
Posted ☆Keep★Moving☆
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OData 带更新的实例,并能取得元数据格式类型相关的知识,希望对你有一定的参考价值。
http://www.cnblogs.com/kid1412/p/6012938.html#CreateANewEntity
<<ABP框架>> OData 集成
本节内容:
OData在odata.org上的定义是:“一个开放的协议,允许创建和使用可查询、可互操作的RESTful api的简单的标准方式”。你可以在ABP里使用OData,Abp.Web.Api.OData的nuget包简化了它的使用方式。
在我们的WebApi项目里,先安装Abp.Web.api.Odata的nuget包:
Install-Package Abp.Web.Api.OData
在我们的模块上设置对AbpWebApiOdataModule的依赖,例如:
(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上获取这个示例项目的源代码。
以上是关于OData 带更新的实例,并能取得元数据格式类型的主要内容,如果未能解决你的问题,请参考以下文章
Olingo Odata v4 - 从模型生成类型、集合和元数据