標籤:
在ASP.NET Web API中使用OData
一.什麼是OData
OData是一個開放的資料協議(Open Data Protocol)
在ASP.NET Web API中,
對於CRUD(create, read, update, and delete)應用比傳統WebAPI增加了很大的靈活性
只要正確使用相關的協議,可以在同等情況下
對一個CRUD應用可以節約很多開發時間,從而提高開發效率
二.怎麼搭建
做一個簡單的訂單查詢樣本
我們使用Code First模式建立兩個實體物件Product(產品),Supplier(供應商)
1.建立一個ASP.NET Empty項目,選擇上使用Web API,如
2.使用NuGet引用OData和EntityFramework程式集
3.在Models檔案夾中增加Product(產品),Supplier(供應商)兩個實體
public class Product { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } public string Category { get; set; } [ForeignKey("Supplier")] public int? SupplierId { get; set; } public virtual Supplier Supplier { get; set; } }public class Supplier { public int Id { get; set; } public string Name { get; set; } public ICollection<Product> Products { get; set; } }
4.增加ProductContext資料庫內容物件,並且在web.config中配置好ConnectionString
public class ProductContext : DbContext { public ProductContext() : base("Demo") { } public DbSet<Product> Products { get; set; } public DbSet<Supplier> Suppliers { get; set; } }
<connectionStrings> <add name="Demo" connectionString="Data Source=(localdb)\v11.0; Initial Catalog=Demo; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|Demo.mdf" providerName="System.Data.SqlClient" /> </connectionStrings>
5.產生資料庫
我們啟動程式包管理主控台,運行如下3個命令,
把我們Code First產生的實體產生到資料庫
PM> Enable-Migrations
PM> Add-Migration FirstInit
PM> Update-Database
然後我們可以在伺服器總管中看到我們產生的資料庫表,如
6.接下來我們在WebApiConfig中註冊我們的OData路由
using Demo2.Models;using System.Web.OData.Builder;using System.Web.OData.Extensions;namespace Demo2{ public static class WebApiConfig { public static void Register(HttpConfiguration config) { ODataModelBuilder builder = new ODataConventionModelBuilder(); builder.EntitySet<Product>("Products"); builder.EntitySet<Supplier>("Suppliers"); config.MapODataServiceRoute("ODataRoute", null, builder.GetEdmModel()); } }}
7.接下來我們分別為Product和Supplier建立ODataController,
我們在中我們選擇好模型類和資料內容,
並重複上面兩步為Supplier實體也產生對應的ODataController
注意:由於VS2013的OData模板中OData V3版本的模板,
引用的名稱空間要從V3
using System.Web.Http;
using System.Web.Http.ModelBinding;
using System.Web.Http.OData;
using System.Web.Http.OData.Routing;
修改為V4
using System.Web.Http;
using System.Web.OData;
到此,我們的OData樣本程式已經搭建完成,我們在瀏覽器裡開啟這個項目的時候
會出現如下
{ "@odata.context":"http://localhost:8914/$metadata","value":[ { "name":"Products","kind":"EntitySet","url":"Products" },{ "name":"Suppliers","kind":"EntitySet","url":"Suppliers" } ]}
表示已經搭建成功了
三.怎麼使用
由於沒有測試資料,我們開始先在資料庫表裡為Products和Suppliers增加一些測試資料
接下來我們看看一些簡單的使用樣本
在自動產生的ProductsController和SuppliersController中
已經為我們產生了如下一些Action
所以對於一些增加,修改,刪除,更新我就不做過多樣本,
這些都是和WebAPI沒有什麼太多區別,
我主要樣本的是查詢的使用,不得不說OData已經為我們把查詢功能全做完了
樣本1:列出所有Product
URL:http://localhost:8914/Products
{
"@odata.context":"http://localhost:8914/$metadata#Products","value":[
{
"Id":1,"Name":"Products1","Price":100.00,"Category":"Test","SupplierId":1
},{
"Id":2,"Name":"Products2","Price":200.00,"Category":"Test","SupplierId":1
},{
"Id":3,"Name":"Products3","Price":300.00,"Category":"Test","SupplierId":1
},{
"Id":4,"Name":"Products4","Price":400.00,"Category":"P1","SupplierId":2
},{
"Id":5,"Name":"Products5","Price":500.00,"Category":"P1","SupplierId":2
},{
"Id":6,"Name":"Products6","Price":600.00,"Category":"P1","SupplierId":2
},{
"Id":7,"Name":"Products7","Price":700.00,"Category":"P1","SupplierId":2
},{
"Id":8,"Name":"Products8","Price":800.00,"Category":"Test","SupplierId":3
},{
"Id":9,"Name":"Products9","Price":900.00,"Category":"P1","SupplierId":3
}
]
}
樣本2,查詢單個Products
URL:http://localhost:8914/Products(1) 其中(1)為Id
{ "@odata.context":"http://localhost:8914/$metadata#Products/$entity","Id":1,"Name":"Products1","Price":100.00,"Category":"Test","SupplierId":1}
樣本3,查詢Products,只列出Name,Price例
URL:http://localhost:8914/Products?$select=Name,Price
{ "@odata.context":"http://localhost:8914/$metadata#Products(Name,Price)","value":[ { "Name":"Products1","Price":100.00 },{ "Name":"Products2","Price":200.00 },{ "Name":"Products3","Price":300.00 },{ "Name":"Products4","Price":400.00 },{ "Name":"Products5","Price":500.00 },{ "Name":"Products6","Price":600.00 },{ "Name":"Products7","Price":700.00 },{ "Name":"Products8","Price":800.00 },{ "Name":"Products9","Price":900.00 } ]}
樣本5:只列出ID為1的Products,只顯示列Name,Price
URL:http://localhost:8914/Products(1)?$select=Name,Price
{ "@odata.context":"http://localhost:8914/$metadata#Products(Name,Price)/$entity","Name":"Products1","Price":100.00}
樣本6:列出Products(只有列Name,Price),包括Supplier
URL:http://localhost:8914/Products?$select=Name,Price&$expand=Supplier
{ "@odata.context":"http://localhost:8914/$metadata#Products(Name,Price,Supplier)","value":[ { "Name":"Products1","Price":100.00,"Supplier":{ "Id":1,"Name":"Supplier1" } },{ "Name":"Products2","Price":200.00,"Supplier":{ "Id":1,"Name":"Supplier1" } },{ "Name":"Products3","Price":300.00,"Supplier":{ "Id":1,"Name":"Supplier1" } },{ "Name":"Products4","Price":400.00,"Supplier":{ "Id":2,"Name":"Supplier2" } },{ "Name":"Products5","Price":500.00,"Supplier":{ "Id":2,"Name":"Supplier2" } },{ "Name":"Products6","Price":600.00,"Supplier":{ "Id":2,"Name":"Supplier2" } },{ "Name":"Products7","Price":700.00,"Supplier":{ "Id":2,"Name":"Supplier2" } },{ "Name":"Products8","Price":800.00,"Supplier":{ "Id":3,"Name":"Supplier3" } },{ "Name":"Products9","Price":900.00,"Supplier":{ "Id":3,"Name":"Supplier3" } } ]}
樣本7:過濾Products,只顯示分類為Test的資料
URL:http://localhost:8914/Products?$filter=Category eq ’Test‘
{ "@odata.context":"http://localhost:8914/$metadata#Products","value":[ { "Id":1,"Name":"Products1","Price":100.00,"Category":"Test","SupplierId":1 },{ "Id":2,"Name":"Products2","Price":200.00,"Category":"Test","SupplierId":1 },{ "Id":3,"Name":"Products3","Price":300.00,"Category":"Test","SupplierId":1 },{ "Id":8,"Name":"Products8","Price":800.00,"Category":"Test","SupplierId":3 } ]}
樣本8:過濾Products,只顯示分類為Test的資料,並排序
URL:http://localhost:8914/Products?$filter=Category eq ’Test‘&$orderby=Price desc
{ "@odata.context":"http://localhost:8914/$metadata#Products","value":[ { "Id":8,"Name":"Products8","Price":800.00,"Category":"Test","SupplierId":3 },{ "Id":3,"Name":"Products3","Price":300.00,"Category":"Test","SupplierId":1 },{ "Id":2,"Name":"Products2","Price":200.00,"Category":"Test","SupplierId":1 },{ "Id":1,"Name":"Products1","Price":100.00,"Category":"Test","SupplierId":1 } ]}
下面是$filter的其它的使用方式
1. http://localhost/Products?$filter=Category eq ‘Test‘
過濾Category=Test
2.http://localhost/Products?$filter=Price lt 10
過濾Price小於10
3。http://localhost/Products?$filter=Price ge 5 and Price le 15
過濾5<=Price>=15
5,還可以使用資料庫函數如:
$filter=substringof(‘zz‘,Name)
$filter=year(ReleaseDate) gt 2005
關於排序
$orderby=Price
$orderby=Price desc
$orderby=Category,Price desc
還有一些過濾器如
$skip,$top,$inlinecount等等
所以OData基本上實現我們們想要查詢的相關方法
對於CURD程式的查詢來說大大提高開發效率
四: 相關引用資源
https://www.asp.net/web-api
https://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-endpoint
轉載請註明出處:http://giantliu.com
ASP.NET Web API中使用OData