本文講述的是簡單的Web API OData使用。一般的,OData應用需要使用到Entity Framwork和database server來實作datasource,在本文中,為了快速的實作OData service,本文使用了in-memory data作為datasource。以下是建立過程。
solution 全覽

建立solution
File->new>Project>Web->Web Application 重命名
安裝NuGet包
- 方法一:運作tools->NuGet package manager->Package manager console
PM> Install-Package Microsoft.AspNet.OData
- 方法二:solution下面的References右鍵Manage NuGet packages ,online查詢并安裝odata應用包
建立Models
在Models檔案夾下建立兩個model class, 分别是Person.cs 和Trip.cs,其中Person model能夠前往(navigate to)Trips.
public class Person
{
[Key] //from System.ComponentModel.DataAnnotations meaning the property is key
public String ID { get; set; }
[Required] // from System.ComponentModel.DataAnnotations meaning the property is required seperately.
public String Name { get; set; }
public String Description { get; set; }
public List<Trip> Trips { get; set; }
}
public class Trip
{
[Key]
public String ID { get; set; }
[Required]
public String Name { get; set; }
}
In-Memory dataSource實作
添加DataSource file->添加DemoDataSources.cs
public class DemoDataSources
{
private static DemoDataSources instance = null;
public static DemoDataSources Instance
{
get { return instance ?? (instance = new DemoDataSources()); }
}
public List<Person> People { get; set; }
public List<Trip> Trips { get; set; }
private DemoDataSources()
{
this.Reset();
this.Initialize();
}
public void Reset()
{
this.People = new List<Person>();
this.Trips = new List<Trip>();
}
public void Initialize()
{ //initialize trips
this.Trips.AddRange(new List<Trip>(){
new Trip(){
ID = "0",
Name = "Trip 0"
},
new Trip(){
ID = "1",
Name = "Trip 1"
},
new Trip(){
ID = "2",
Name = "Trip 2"
}
});
//initialize people
this.People.AddRange(new List<Person>(){
new Person(){
ID = "001",
Name = "Demi",
Trips = new List<Trip>{Trips[0], Trips[1]}
},
new Person(){
ID = "002",
Name = "Claire",
Description = "This is a beautiful girl!",
Trips = new List<Trip>{Trips[1], Trips[2],Trips[0]}
}
});
}
}
添加controllers
前面model中有兩個entity set(實體集),在這裡我們就需要對應建立兩個controller,分别是PeopleController.cs和TripsController.cs。本文中隻是給出了簡單的Get查詢方法,也可以增加其他的Odata操作方法,并且實作方法與使用EF實作的DataSource中類似。
[EnableQuery]
public class PeopleController : ODataController
{
public IHttpActionResult Get()
{
return Ok(DemoDataSources.Instance.People.AsQueryable());
}
}
[EnableQuery]
public class TripsController : ODataController
{
public IHttpActionResult Get()
{
return Ok(DemoDataSources.Instance.Trips.AsQueryable());
}
}
配置endpoint
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
//config.MapHttpAttributeRoutes();
//config.Routes.MapHttpRoute(
//name: "DefaultApi",
//routeTemplate: "api/{controller}/{id}",
// defaults: new { id = RouteParameter.Optional }
//);
config.MapODataServiceRoute("odata", null, GetEdmModel(), new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));
config.EnsureInitialized();
}
private static IEdmModel GetEdmModel()
{
var builder = new ODataConventionModelBuilder {Namespace = "Demos", ContainerName = "DefaultContainer"};
builder.EntitySet<Person>("People");
builder.EntitySet<Trip>("Trips");
var edmModel = builder.GetEdmModel();
return edmModel;
}
運作效果
所有的GET方法
http://localhost:26388 - service document
http://localhost:26388/$metadata - service metadata
http://localhost:26388/People - get all
http://localhost:26388/People?$filter=contains(Description,'girl') - filter
http://localhost:26388/People?$select=Name - select
http://localhost:26388/People?$expand=Trips - expand