OData是什麼
Open Data Protocol(開放數據協議,OData)是用來查詢和更新數據的一種Web協議,其提供了把存在於應用程序中的數據暴露出來的方式。OData運用且構建於很多Web技術之上,比如HTTP、Atom Publishing Protocol(AtomPub)和JSON,提供了從各種應用程序、服務和存儲庫中訪問信息的能力。OData被用來從各種數據源中暴露和訪問信息,這些數據源包括但不限於:關係數據庫、文件系統、內容管理系統和傳統Web站點。更多詳細定義可以查閱OData官網,接下來用示例看看OData是怎麼讓查詢隨心所欲。
示例
新建一個.NET CORE 3+ WEBAPI 項目,安裝 Microsoft.AspNetCore.OData 及其所有依賴項
添加測試模型Student,用來進行數據查詢
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
添加數據調用控制器,繼承 ControllerBase ,添加Get 方法以便於插敘Student 數據,添加 [EnableQuery] ,用來支持OData查詢選項。
[Route("api/[controller]")]
public class TestController : ControllerBase
{
private List<Student> students = new List<Student>()
{
new Student()
{
Id = 1,
Name = "張三",
Age = 18,
},
new Student()
{
Id = 2,
Name = "李四",
Age = 88,
},
new Student()
{
Id = 3,
Name = "趙五",
Age = 20,
},
new Student()
{
Id = 4,
Name = "王六",
Age = 42,
}
};
[EnableQuery]
[HttpGet]
public List<Student> Get()
{
return students;
}
}
接下來在Startup 配置OData
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
//添加OData
services.AddOData();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
//配置OData 路由節點
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.EnableDependencyInjection();
endpoints.Filter().Count().Expand().OrderBy().Select().MaxTop(null).;
});
}
這裏需要注意的是,查閱了很多文檔資料,都是用的以下配置,先禁用掉了控制器的端點路由配置,然後在Configure中使用MVC路由配置,這樣也是可以了,但是OData7.4版本已經支持端點路由配置了,也沒有必要那樣去做了
//不推薦寫法
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers(mvcOptions=>mvcOption.EnableEndpointRouting = false);
//添加OData
services.AddOData();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseMvc(routeBuilder =>
{
routeBuilder.EnableDependencyInjection();
routeBuilder.Filter().Count().Expand().OrderBy().Select().MaxTop(null).;
});
}
現在可以在數據上嘗試orderby,count,top的常規操作,結果如圖所示:
- 默認情況下 /api/test
- $ orderby /api/test?$orderby=age desc
$ orderby /api/test?$filter=age eq 42
filter 語法條件列表
條件 | 備註 | 示例 |
---|---|---|
eq | 等於 | $filter=priority et 1 |
ne | 不等於 | $filter=priority ne 1 |
gt | 大於 | $filter=priority gt 1 |
ge | 大於或等於 | $filter=priority ge 1 |
lt | 少於 | $filter=priority lt 1 |
le | 小於或等於 | $filter=priority le 1 |
and | 並且 | $filter=priority gt 1 and priority lt 10 |
or | 或者 | $filter=priority gt 1 or priority lt 10 |
not | 不是 | $filter=not endswith(name,‘task’) |
- $ skip& $ top /api/test? $skip=2& $top=2
但是在執行 select 的時候數據出現了問題
這是因爲採用非Edm路線配置OData,則需要安裝Microsoft.AspNetCore.Mvc.NewtonsoftJson 軟件包來解決Json格式問題 ,然後修改Startup文件ConfigureService 以啓用Json格式擴展方法
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers().AddNewtonsoftJson();
services.AddOData();
}
配置完成之後我們在來看看select 結果,很顯然數據有了變動,只查出了name字段
這裏順便在提一下Edm路線配置OData,主要區別在與OData路由策略的配置
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.Select().Filter().OrderBy().Count().MaxTop(null);
endpoints.MapODataRoute("odata", "odata", GetEdmModel());
});
}
private IEdmModel GetEdmModel()
{
var odataBuilder = new ODataConventionModelBuilder();
odataBuilder.EntitySet<Student>("Student");
return odataBuilder.GetEdmModel();
}
總結
現在OData 在.NET CORE 3.1的配置已經初步配置完,是不是覺得數據查詢變的隨心所欲,在也不需要爲了需求的變動來回修改Dto了,OData 的語法遠遠不止這些,如需瞭解請移步到官網查看更多語法