本文讲述的是简单的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