.NET Core開(kāi)發(fā)日志之OData(Open Data Protocol)
簡(jiǎn)述
OData,即Open Data Protocol,是由微軟在2007年推出的一款開(kāi)放協(xié)議,旨在通過(guò)簡(jiǎn)單、標(biāo)準(zhǔn)的方式創(chuàng)建和使用查詢式及交互式RESTful API。
類庫(kù)
在.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來(lái)描述數(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();
}注冊(cè)O(shè)Data服務(wù)
在Startup文件的ConfigureServices方法里注冊(cè)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é)點(diǎn),所以要使用OData的終結(jié)點(diǎn)的話需要將默認(rèn)選項(xiàng)禁用掉。
注冊(cè)O(shè)Data終結(jié)點(diǎn)
同樣在Startup文件里,在其Configure方法內(nèi)將原來(lái)的注冊(cè)路由內(nèi)容改為注冊(cè)O(shè)Data的終結(jié)點(diǎn)。
app.UseMvc(b =>
{
b.MapODataServiceRoute("odata", "odata", GetEdmModel());
});顯示元數(shù)據(jù)
運(yùn)行程序后訪問(wèn)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í)例中不考慮數(shù)據(jù)庫(kù)的操作,故而使用hard code方式構(gòu)建必要的模型對(duì)象。
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特性在需要高級(jí)查詢的場(chǎng)景時(shí)必須添加。
查詢
加入Controller之后,訪問(wèn)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"
}
}
]
}訪問(wèn)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"
}
}高級(jí)查詢
如果想要使用OData查詢的高級(jí)功能,可以在注冊(cè)終結(jié)點(diǎn)時(shí)額外加上相應(yīng)的配置。
app.UseMvc(b =>
{
b.Select().Expand().Filter().OrderBy().MaxTop(100).Count();
b.MapODataServiceRoute("odata", "odata", GetEdmModel());
});訪問(wèn)網(wǎng)址時(shí)加上所需的查詢內(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"
}
]
}如果想要按特定條件過(guò)濾數(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"
}
}
]
}總結(jié)
不難看出,OData的真正魅力在于其對(duì)那些高級(jí)查詢功能的支持,所以在創(chuàng)建RESTful API時(shí),不妨考慮使用OData,這樣應(yīng)該能減少許多不必要的代碼工作。
好了,以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
ASP.NET MVC 導(dǎo)出Word報(bào)表
本文主要介紹了ASP.NET MVC 導(dǎo)出Word報(bào)表的方法,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02
WinForm中窗體間的數(shù)據(jù)傳遞交互的一些方法
通過(guò)子窗口向外引發(fā)一個(gè)事件,父窗口去實(shí)現(xiàn)該事件,我們可以再不關(guān)閉父窗口和子窗口的情況下進(jìn)行數(shù)據(jù)的傳輸顯示2012-12-12
ASP.NET中MVC使用AJAX調(diào)用JsonResult方法并返回自定義錯(cuò)誤信息
這篇文章主要介紹了ASP.NET中MVC使用AJAX調(diào)用JsonResult方法并返回自定義錯(cuò)誤信息的相關(guān)資料,需要的朋友可以參考下2014-11-11
解決.NET?Core企業(yè)微信openapi回調(diào)地址請(qǐng)求不通過(guò)的問(wèn)題
這篇文章介紹了解決.NET?Core企業(yè)微信openapi回調(diào)地址請(qǐng)求不通過(guò)的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
ASP.NET顯示農(nóng)歷時(shí)間改進(jìn)版
這篇文章主要介紹了ASP.NET顯示農(nóng)歷時(shí)間改進(jìn)版,是針對(duì)前面一篇ASP.NET顯示農(nóng)歷時(shí)間的改進(jìn)版,實(shí)現(xiàn)了比較簡(jiǎn)單的封裝,增加了易用性,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-11-11
asp.net下模態(tài)對(duì)話框關(guān)閉之后繼續(xù)執(zhí)行服務(wù)器端代碼的問(wèn)題
asp.net下模態(tài)對(duì)話框關(guān)閉之后繼續(xù)執(zhí)行服務(wù)器端代碼的問(wèn)題...2007-04-04
asp.NET中實(shí)現(xiàn)文件的壓縮和解壓(3種方式)
本篇文章主要介紹了asp.NET中實(shí)現(xiàn)文件的壓縮和解壓,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2016-11-11
詳解最好的.NET開(kāi)源免費(fèi)ZIP庫(kù)DotNetZip(.NET組件介紹之三)
本篇文章主要介紹了.NET開(kāi)源免費(fèi)ZIP庫(kù)DotNetZip組件的介紹,可以實(shí)現(xiàn)對(duì)文件的壓縮和解壓,有興趣的朋友可以了解一下。2016-12-12

