.NET Core开发日志之OData(Open Data Protocol)

OData,即Open Data Protocol,是由微软在2007年推出的一款开放协议,旨在通过简单、标准的方式创建和使用查询式及交互式RESTful API。

类库

在.NET Core中想要使用OData功能的话需要添加Microsoft.AspNetCore.OData包。

dotnet add package Microsoft.AspNetCore.OData

准备模型类

public class Address { public string City { get; set; } public string Street { get; set; } } public enum Category { Book, Magazine, EBook } public class Press { public int Id { get; set; } public string Name { get; set; } public string Email { get; set; } public Category Category { get; set; } } public class Book { public int Id { get; set; } public string ISBN { get; set; } public string Title { get; set; } public string Author { get; set; } public decimal Price { get; set; } public Address Address { get; set; } public Press Press { get; set; } }

创建Edm模型

OData使用EDM,即Entity Data Model来描述数据的结构。在Startup文件中添加创建方法。

private static IEdmModel GetEdmModel() { var builder = new ODataConventionModelBuilder(); builder.EntitySet<Book>("Books"); builder.EntitySet<Press>("Presses"); return builder.GetEdmModel(); }

注册OData服务

在Startup文件的ConfigureServices方法里注册OData服务。

services.AddOData(); services.AddMvc(options => { options.EnableEndpointRouting = false; }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

这里要注意的是在.NET Core 2.2里,默认已经有终结点,所以要使用OData的终结点的话需要将默认选项禁用掉。

注册OData终结点

同样在Startup文件里,在其Configure方法内将原来的注册路由内容改为注册OData的终结点。

app.UseMvc(b => { b.MapODataServiceRoute("odata", "odata", GetEdmModel()); });

显示元数据

运行程序后访问https://localhost:5001/odata/$metadata地址,可以看到所有可用模型的元数据。

<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0"> <edmx:DataServices> <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="Default"> <EntityType> <Key> <PropertyRef/> </Key> <Property Type="Edm.Int32" Nullable="false"/> <Property Type="Edm.String"/> <Property Type="Edm.String"/> <Property Type="Edm.String"/> <Property Type="Edm.Decimal" Nullable="false"/> <Property Type="Default.Address"/> <NavigationProperty Type="Default.Press"/> </EntityType> <EntityType> <Key> <PropertyRef/> </Key> <Property Type="Edm.Int32" Nullable="false"/> <Property Type="Edm.String"/> <Property Type="Edm.String"/> <Property Type="Default.Category" Nullable="false"/> </EntityType> <ComplexType> <Property Type="Edm.String"/> <Property Type="Edm.String"/> </ComplexType> <EnumType> <Member Value="0"/> <Member Value="1"/> <Member Value="2"/> </EnumType> <EntityContainer> <EntitySet EntityType="Default.Book"> <NavigationPropertyBinding Path="Press" Target="Presses"/> </EntitySet> <EntitySet EntityType="Default.Press"/> </EntityContainer> </Schema> </edmx:DataServices> </edmx:Edmx>

创建Controller

本文实例中不考虑数据库的操作,故而使用hard code方式构建必要的模型对象。

public class BooksController : ODataController { private static IList<Book> Books {get; set;} public BooksController() { Books = new List<Book> { new Book { Id = 1, ISBN = "111-0-321-56789-1", Title = "Calculus", Price = 66.6m, Address = new Address { City = "Shanghai", Street = "Beijin Xi Road" }, Press = new Press { Id = 1, Name = "Shanghai Tongji", Category = Category.Book } }, new Book { Id = 2, ISBN = "222-2-654-00000-2", Title = "Linear Algebra", Price = 53.2m, Address = new Address { City = "Shanghai", Street = "Beijin Dong Road" }, Press = new Press { Id = 2, Name = "Shanghai Fudan", Category = Category.EBook } } }; } [EnableQuery] public IActionResult Get() { return Ok(Books); } [EnableQuery] public IActionResult Get(int key) { return Ok(Books.FirstOrDefault(b => b.Id == key)); } }

EnableQuery特性在需要高级查询的场景时必须添加。

查询

加入Controller之后,访问https://localhost:5001/odata/Books地址,可得到所有Book数据。

{ "@odata.context": "https://localhost:5001/odata/$metadata#Books", "value": [ { "Id": 1, "ISBN": "111-0-321-56789-1", "Title": "Calculus", "Author": null, "Price": 66.6, "Address": { "City": "Shanghai", "Street": "Beijin Xi Road" } }, { "Id": 2, "ISBN": "222-2-654-00000-2", "Title": "Linear Algebra", "Author": null, "Price": 53.2, "Address": { "City": "Shanghai", "Street": "Beijin Dong Road" } } ] }

访问https://localhost:5001/odata/Books(1)地址,可得到key值为1的Book数据。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdsdwz.html