Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of OData of .NET Core Development Log

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

这篇文章主要为大家展示了".NET Core开发日志之OData的示例分析",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下".NET Core开发日志之OData的示例分析"这篇文章吧。

简述

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("Books"); builder.EntitySet("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地址,可以看到所有可用模型的元数据。

创建Controller

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

public class BooksController : ODataController{ private static IList Books {get; set;} public BooksController() { Books = new List { 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数据。

{ "@odata.context": "https://localhost:5001/odata/$metadata#Books/$entity", "Id": 1, "ISBN": "111-0-321-56789-1", "Title": "Calculus", "Author": null, "Price": 66.6, "Address": { "City": "Shanghai", "Street": "Beijin Xi Road" }}

高级查询

如果想要使用OData查询的高级功能,可以在注册终结点时额外加上相应的配置。

app.UseMvc(b =>{ b.Select().Expand().Filter().OrderBy().MaxTop(100).Count(); b.MapODataServiceRoute("odata", "odata", GetEdmModel());});

访问网址时加上所需的查询内容:

https://localhost:5001/odata/Books?$select=Id,Title

{ "@odata.context": "https://localhost:5001/odata/$metadata#Books(Id,Title)", "value": [ { "Id": 1, "Title": "Calculus" }, { "Id": 2, "Title": "Linear Algebra" } ]}

如果想要按特定条件过滤数据内容的话也很容易:

https://localhost:5001/odata/Books?$filter=Price%20le%2060

{ "@odata.context": "https://localhost:5001/odata/$metadata#Books", "value": [ { "Id": 2, "ISBN": "222-2-654-00000-2", "Title": "Linear Algebra", "Author": null, "Price": 53.2, "Address": { "City": "Shanghai", "Street": "Beijin Dong Road" } } ]}以上是".NET Core开发日志之OData的示例分析"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 264

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report