這篇文章主要為大家展示了“.NET Core開發(fā)日志之OData的示例分析”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“.NET Core開發(fā)日志之OData的示例分析”這篇文章吧。

簡述
OData,即Open Data Protocol,是由微軟在2007年推出的一款開放協(xié)議,旨在通過簡單、標(biāo)準(zhǔn)的方式創(chuàng)建和使用查詢式及交互式RESTful API。
類庫
在.NET Core中想要使用OData功能的話需要添加Microsoft.AspNetCore.OData包。
dotnet add package Microsoft.AspNetCore.OData
準(zhǔn)備模型類
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; }
}創(chuàng)建Edm模型
OData使用EDM,即Entity Data Model來描述數(shù)據(jù)的結(jié)構(gòu)。在Startup文件中添加創(chuàng)建方法。
private static IEdmModel GetEdmModel()
{
var builder = new ODataConventionModelBuilder();
builder.EntitySet<Book>("Books");
builder.EntitySet<Press>("Presses");
return builder.GetEdmModel();
}注冊O(shè)Data服務(wù)
在Startup文件的ConfigureServices方法里注冊O(shè)Data服務(wù)。
services.AddOData();
services.AddMvc(options =>
{
options.EnableEndpointRouting = false;
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);這里要注意的是在.NET Core 2.2里,默認(rèn)已經(jīng)有終結(jié)點,所以要使用OData的終結(jié)點的話需要將默認(rèn)選項禁用掉。
注冊O(shè)Data終結(jié)點
同樣在Startup文件里,在其Configure方法內(nèi)將原來的注冊路由內(nèi)容改為注冊O(shè)Data的終結(jié)點。
app.UseMvc(b =>
{
b.MapODataServiceRoute("odata", "odata", GetEdmModel());
});顯示元數(shù)據(jù)
運行程序后訪問https://localhost:5001/odata/$metadata地址,可以看到所有可用模型的元數(shù)據(jù)。
<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 Name="Book"> <Key> <PropertyRef Name="Id"/> </Key> <Property Name="Id" Type="Edm.Int32" Nullable="false"/> <Property Name="ISBN" Type="Edm.String"/> <Property Name="Title" Type="Edm.String"/> <Property Name="Author" Type="Edm.String"/> <Property Name="Price" Type="Edm.Decimal" Nullable="false"/> <Property Name="Address" Type="Default.Address"/> <NavigationProperty Name="Press" Type="Default.Press"/> </EntityType> <EntityType Name="Press"> <Key> <PropertyRef Name="Id"/> </Key> <Property Name="Id" Type="Edm.Int32" Nullable="false"/> <Property Name="Name" Type="Edm.String"/> <Property Name="Email" Type="Edm.String"/> <Property Name="Category" Type="Default.Category" Nullable="false"/> </EntityType> <ComplexType Name="Address"> <Property Name="City" Type="Edm.String"/> <Property Name="Street" Type="Edm.String"/> </ComplexType> <EnumType Name="Category"> <Member Name="Book" Value="0"/> <Member Name="Magazine" Value="1"/> <Member Name="EBook" Value="2"/> </EnumType> <EntityContainer Name="Container"> <EntitySet Name="Books" EntityType="Default.Book"> <NavigationPropertyBinding Path="Press" Target="Presses"/> </EntitySet> <EntitySet Name="Presses" EntityType="Default.Press"/> </EntityContainer> </Schema> </edmx:DataServices> </edmx:Edmx>
創(chuàng)建Controller
本文實例中不考慮數(shù)據(jù)庫的操作,故而使用hard code方式構(gòu)建必要的模型對象。
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數(shù)據(jù)。
{
"@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數(shù)據(jù)。
{
"@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查詢的高級功能,可以在注冊終結(jié)點時額外加上相應(yīng)的配置。
app.UseMvc(b =>
{
b.Select().Expand().Filter().OrderBy().MaxTop(100).Count();
b.MapODataServiceRoute("odata", "odata", GetEdmModel());
});訪問網(wǎng)址時加上所需的查詢內(nèi)容:
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"
}
]
}如果想要按特定條件過濾數(shù)據(jù)內(nèi)容的話也很容易:
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開發(fā)日志之OData的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
當(dāng)前題目:.NETCore開發(fā)日志之OData的示例分析-創(chuàng)新互聯(lián)
文章地址:http://www.chinadenli.net/article22/dodsjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、定制網(wǎng)站、App開發(fā)、微信小程序、手機網(wǎng)站建設(shè)、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容