簡(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>
新聞熱點(diǎn)
疑難解答
圖片精選