[转]Web API OData V4 Keys, Composite Keys and Functions Part 11
本文转自:https://damienbod.com/2014/09/12/web-api-odata-v4-keys-composite-keys-and-functions-part-11/
Web API OData V4 Keys, Composite Keys and Functions Part 11
This article demonstrates how OData Functions can be used together with entities which have simple keys, composite keys, entity collections or used as plain simple global functions.
Part 1 Getting started with Web API and OData V4 Part 1.
Part 2 Web API and OData V4 Queries, Functions and Attribute Routing Part 2
Part 3Web API and OData V4 CRUD and Actions Part 3
Part 4 Web API OData V4 Using enum with Functions and Entities Part 4
Part 5 Web API OData V4 Using Unity IoC, SQLite with EF6 and OData Model Aliasing Part 5
Part 6 Web API OData V4 Using Contained Models Part 6
Part 7 Web API OData V4 Using a Singleton Part 7
Part 8 Web API OData V4 Using an OData T4 generated client Part 8
Part 9 Web API OData V4 Caching Part 9
Part 10 Web API OData V4 Batching Part 10
Part 11 Web API OData V4 Keys, Composite Keys and Functions Part 11
Code: https://github.com/damienbod/WebApiODataFunctionsAndKeys
Basic Model and Entities
To demonstrate the different functions, with the different model types, a simple OData model is used with 2 Entities, a Stadium entity and a City entity. A namespace ‘D’ is defined because functions require a namespace. If not defined, the ‘Default’ namespace is used. I would prefer to use no namespace in the URL but this is not possible.
public static IEdmModel GetModel()
{
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.Namespace = "D";
builder.ContainerName = "Default";
EntitySetConfiguration<City> cities = builder.EntitySet<City>("City");
EntitySetConfiguration<Stadium> stadiums = builder.EntitySet<Stadium>("Stadium");
The City entity has just a simple key property. This is the normal form.
[DataContract(Name = "City")]
public class City
{
[DataMember]
[Key]
public int Id { get; set; } [DataMember]
public long Population { get; set; } [DataMember]
public string Country { get; set; }
}
The Stadium entity has a composite key made up of 2 key properties.
[DataContract(Name = "Stadium")]
public class Stadium
{
[DataMember]
[Key]
public string Name { get; set; } [DataMember]
[Key]
public string Country { get; set; } [DataMember]
public int Capacity { get; set; } [DataMember]
public string Owner { get; set; }
}
Web Configuration for IIS
The web.config needs some special configuration, if you want that the function URLs are reachable in the IIS. If not configured, you receive 404 for your URLs because the ‘.’ is not evaluated. The runAllManagedModulesForAllRequests is set to true.
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"></modules>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
OData Global Functions
These functions are the most simple of all the OData functions to setup. The functions are global and not connected to any entity or collection of entities. The global functions can be added directly to the model builder.
builder.Function("GlobalFunction")
.ReturnsCollectionFromEntitySet<Stadium>("Stadium");
The global functions can then be made public inside any ODataController, the route must match the OData routes. This can be checked using the ~/odata/$metadata URL.
using System.Collections.Generic;
using System.Web.Http;
using System.Web.OData;
using System.Web.OData.Query;
using System.Web.OData.Routing;
using WebAPIODataKeys.Models; namespace WebApiODataKeys.Controllers
{
public class GlobalOdataFunctionsActionsController : ODataController
{
[EnableQuery(PageSize = 20, AllowedQueryOptions = AllowedQueryOptions.All)]
[ODataRoute("GlobalFunction()")]
[HttpGet]
public IHttpActionResult GlobalFunction()
{
return Ok(new List<Stadium>{ new Stadium { Capacity = 2300, Country = "Switzerland", Name = "Times Stadium", Owner = "Global Owner" } });
}
}
}
~/odata/GlobalFunction()

Collection Function which returns a Collection
OData functions can also be added to collections of entities. The following model configuration adds a GetStadiumsWithFunction function to the Stadium entity collection and returns a collection of stadium entities.
FunctionConfiguration getStadiumsWithFunction = stadiums.EntityType.Collection.Function("GetStadiumsWithFunction");
getStadiumsWithFunction.ReturnsCollectionFromEntitySet<Stadium>("Stadium");
The function is then made public in the Stadium ODataController. The URL contains the function namespace.
[EnableQuery(PageSize = 20, AllowedQueryOptions = AllowedQueryOptions.All)]
[ODataRoute("D.GetStadiumsWithFunction()")]
[HttpGet]
public IHttpActionResult GetStadiumsWithFunction()
{
return Ok(new List<Stadium> { new Stadium { Capacity = 2300, Country = "Switzerland", Name = "Times Stadium", Owner = "FC Zug" } });
}
The function can then be used as follows:
~/odata/Stadium/D.GetStadiumsWithFunction()

Collection Function which returns an Entity
This is very similar to the previous functions except this function has 2 input parameters and returns a single Stadium entity.
Function definition in the model:
// http://localhost:60096/odata/Stadium/D.GetStadiumTest(test='ddd', land='ssss')
FunctionConfiguration getStadiumsTest = stadiums.EntityType.Collection.Function("GetStadiumTest");
getStadiumsTest.Parameter<string>("test");
getStadiumsTest.Parameter<string>("name");
getStadiumsTest.ReturnsFromEntitySet<Stadium>("Stadium");
Function definition in the controller:
// http://localhost:60096/odata/Stadium/D.GetStadiumTest(test='ddd', land='ssss')
[ODataRoute("D.GetStadiumTest(test={test}, name={name})")]
[EnableQuery(PageSize = 20, AllowedQueryOptions = AllowedQueryOptions.All)]
[HttpGet]
public IHttpActionResult GetStadiumTest([FromODataUri] string test, [FromODataUri] string name)
{
return Ok(new List<Stadium> { new Stadium { Capacity = 2300, Country = name, Name = test, Owner = "FC Zug" } });
}
~/odata/Stadium/D.GetStadiumTest(test=’ddd’, land=’ssss’)
Entity Get with single key Entity
Before a function can be returned for a single entity, we need to know how to return the single entity in the controller. The key word can be used to match the single key property.
public IHttpActionResult Get(int key)
{
return Ok(new City { Population = 9000000, Country = "Berlin", Id = key });
}
This can then be used as follows:
~/odata/City(8)
Entity Function (Single key property)
The following example shows how to map a function to a single entity from type City. The function returns a Stadium entity.
FunctionConfiguration getStadiumFromCityWithFunction = cities.EntityType.Function("GetStadiumsFromCityWithFunction");
getStadiumFromCityWithFunction.ReturnsCollectionFromEntitySet<Stadium>("Stadium");
The function is added to the city controller, namespace included.
[EnableQuery(PageSize = 20, AllowedQueryOptions = AllowedQueryOptions.All)]
[ODataRoute("City({key})/D.GetStadiumsFromCityWithFunction()")]
[HttpGet]
public IHttpActionResult GetStadiumsFromCityWithFunction(int key)
{
return Ok(new List<Stadium> { new Stadium { Capacity = 2300, Country = "Switzerland", Name = "Times Stadium", Owner = "FC Zug" } });
}
The function can be called as follows:
~/odata/City(8)/D.GetStadiumsFromCityWithFunction()
Entity Get with Composite keys Entity
The following controller method shows how to GET the composite Key entity. The key word cannot be used here because it has 2 separate key properties. The key properties have to be defined in the URL.
// http://localhost:60096/odata/Stadium(Name='Baz', Country='Germany')
[ODataRoute("(Name={name}, Country={country})")]
[EnableQuery(PageSize = 20, AllowedQueryOptions = AllowedQueryOptions.All)]
[HttpGet]
public IHttpActionResult Get([FromODataUri] string name, [FromODataUri] string country)
{
return Ok(new Stadium { Capacity = 2300, Country = country, Name = name, Owner = "FC Zug" });
}
This can be called as follows:
~/odata/Stadium(Name=’Baz’, Country=’Germany’)
Entity Function with Composite keys Entity
Now that we know how to return a single entity for a composite entity, we can add a function to this and use it. The following method adds a function to the composite key entity Stadium.
FunctionConfiguration getCityFromStadiumWithFunction = stadiums.EntityType.Function("GetCityFromStadiumWithFunction");
getCityFromStadiumWithFunction.ReturnsFromEntitySet<City>("City");
This is then used in the Stadium ODataController.
// http://localhost:60096/odata/Stadium(Name='Baz', Country='Germany')/D.GetCityFromStadiumWithFunction()
[EnableQuery(PageSize = 20, AllowedQueryOptions = AllowedQueryOptions.All)]
[ODataRoute("(Name={name},Country={country})/D.GetCityFromStadiumWithFunction()")]
[HttpGet]
public IHttpActionResult GetCityFromStadiumWithFunction([FromODataUri] string name, [FromODataUri] string country)
{
return Ok(new City { Population = 9000000, Country = "Berlin", Id = 8 });
}
This can then be called:
~/odata/Stadium(Name=’Baz’, Country=’Germany’)/D.GetCityFromStadiumWithFunction()
The full code example can be download with this link.
As you can see, OData Functions are very easy to use once you understand the design constraints and how the routing works.
Complete OData Model
public static IEdmModel GetModel()
{
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.Namespace = "D";
builder.ContainerName = "Default"; EntitySetConfiguration<City> cities = builder.EntitySet<City>("City");
EntitySetConfiguration<Stadium> stadiums = builder.EntitySet<Stadium>("Stadium"); // Per Collection Stadium
FunctionConfiguration getStadiumsWithFunction = stadiums.EntityType.Collection.Function("GetStadiumsWithFunction");
getStadiumsWithFunction.ReturnsCollectionFromEntitySet<Stadium>("Stadium"); // Per Collection Stadium, returns single entity
FunctionConfiguration getStadiumsTest = stadiums.EntityType.Collection.Function("GetStadiumTest");
getStadiumsTest.Parameter<string>("test");
getStadiumsTest.Parameter<string>("name");
getStadiumsTest.ReturnsFromEntitySet<Stadium>("Stadium"); // Per Entity (Single key property) City
FunctionConfiguration getStadiumFromCityWithFunction = cities.EntityType.Function("GetStadiumsFromCityWithFunction");
getStadiumFromCityWithFunction.ReturnsCollectionFromEntitySet<Stadium>("Stadium"); // Per Entity composite key Stadium
FunctionConfiguration getCityFromStadiumWithFunction = stadiums.EntityType.Function("GetCityFromStadiumWithFunction");
getCityFromStadiumWithFunction.ReturnsFromEntitySet<City>("City"); // Global Function
builder.Function("GlobalFunction").ReturnsCollectionFromEntitySet<Stadium>("Stadium"); return builder.GetEdmModel();
}
Complete OData StadiumController
using System.Collections.Generic;
using System.Web.Http;
using System.Web.OData;
using System.Web.OData.Query;
using System.Web.OData.Routing;
using WebAPIODataKeys.Models; namespace WebApiODataKeys.Controllers
{
[ODataRoutePrefix("Stadium")]
public class StadiumController : ODataController
{
[EnableQuery(PageSize = 20, AllowedQueryOptions = AllowedQueryOptions.All)]
[HttpGet]
[ODataRoute]
public IHttpActionResult Get()
{
return Ok(new List<Stadium> { new Stadium { Capacity = 2300, Country = "Switzerland", Name = "Times Stadium", Owner = "FC Zug" } });
} // GET odata/stadium?name=fds&country=Switzerland
//[EnableQuery(PageSize = 20, AllowedQueryOptions = AllowedQueryOptions.All)]
//[HttpGet]
//public IHttpActionResult Get(string name, string country)
//{
// return Ok(new Stadium { Capacity = 2300, Country = "Switzerland", Name = "Times Stadium", Owner = "FC Zug" });
//} // http://localhost:60096/odata/Stadium(Name='Baz', Country='Germany')
[ODataRoute("(Name={name}, Country={country})")]
[EnableQuery(PageSize = 20, AllowedQueryOptions = AllowedQueryOptions.All)]
[HttpGet]
public IHttpActionResult Get([FromODataUri] string name, [FromODataUri] string country)
{
return Ok(new Stadium { Capacity = 2300, Country = country, Name = name, Owner = "FC Zug" });
} // http://localhost:60096/odata/Stadium/D.GetStadiumTest(test='ddd', land='ssss')
[ODataRoute("D.GetStadiumTest(test={test}, name={name})")]
[EnableQuery(PageSize = 20, AllowedQueryOptions = AllowedQueryOptions.All)]
[HttpGet]
public IHttpActionResult GetStadiumTest([FromODataUri] string test, [FromODataUri] string name)
{
return Ok(new List<Stadium> { new Stadium { Capacity = 2300, Country = name, Name = test, Owner = "FC Zug" } });
} [EnableQuery(PageSize = 20, AllowedQueryOptions = AllowedQueryOptions.All)]
[ODataRoute("D.GetStadiumsWithFunction()")]
[HttpGet]
public IHttpActionResult GetStadiumsWithFunction()
{
return Ok(new List<Stadium> { new Stadium { Capacity = 2300, Country = "Switzerland", Name = "Times Stadium", Owner = "FC Zug" } });
} // http://localhost:60096/odata/Stadium(Name='Baz', Country='Germany')/D.GetCityFromStadiumWithFunction()
[EnableQuery(PageSize = 20, AllowedQueryOptions = AllowedQueryOptions.All)]
[ODataRoute("(Name={name},Country={country})/D.GetCityFromStadiumWithFunction()")]
[HttpGet]
public IHttpActionResult GetCityFromStadiumWithFunction([FromODataUri] string name, [FromODataUri] string country)
{
return Ok(new City { Population = 9000000, Country = "Berlin", Id = 8 });
}
}
}
Complete OData CityController
using System.Collections.Generic;
using System.Web.Http;
using System.Web.OData;
using System.Web.OData.Query;
using System.Web.OData.Routing;
using WebAPIODataKeys.Models; namespace WebApiODataKeys.Controllers
{
public class CityController : ODataController
{
public IHttpActionResult Get()
{
return Ok(new List<City> { new City { Population = 2300000, Country = "Switzerland", Id=1} });
} public IHttpActionResult Get(int key)
{
return Ok(new City { Population = 9000000, Country = "Berlin", Id = key });
} [EnableQuery(PageSize = 20, AllowedQueryOptions = AllowedQueryOptions.All)]
[ODataRoute("City({key})/D.GetStadiumsFromCityWithFunction()")]
[HttpGet]
public IHttpActionResult GetStadiumsFromCityWithFunction(int key)
{
return Ok(new List<Stadium> { new Stadium { Capacity = 2300, Country = "Switzerland", Name = "Times Stadium", Owner = "FC Zug" } });
}
}
}
Links:
http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-routing-conventions
http://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/OData/v4/
http://blogs.msdn.com/b/odatateam/
[转]Web API OData V4 Keys, Composite Keys and Functions Part 11的更多相关文章
- ASP.NET 4.x Web Api Odata v4 backend modify query 修改查询
有时候我们会想给予权限添加 filter 到查询上. 比如 会员和管理员都使用了 /api/products 作为 product 查询 但是会员不应该可以看见还没有上架的货品 /api/produc ...
- [转]How to Use Web API OData to Build an OData V4 Service without Entity Framework
本文转自:http://www.odata.org/blog/how-to-use-web-api-odata-to-build-an-odata-v4-service-without-entity- ...
- node-odata: ASP.NET WEB API OData的替代品
什么是 OData 协议? OData, 相信身为 .NET 程序员应该不为陌生, 尤其是它的实现: ASP.NET WEB API OData. 对于 OData, 官网上对其的定义是 OData ...
- vs2012 + web api + OData + EF + MYsql
vs2012 + web api + OData + EF + MYsql 开发及部署 先说下我的情况,b/s开发这块已经很久没有搞了,什么web api .MVC.OData都只是听过,没有实际开发 ...
- 使用ASP.Net MVC5 Web API OData和Sencha Touch 开发WebAPP
使用ASP.Net MVC5 Web API OData和SenCha Touch 开发WebAPP Demo 效果 第一步 创建数据库 创建表 第二步 搭建MVC,并导入OData 第三步,写入We ...
- [转]Support Composite Key in ASP.NET Web API OData
本文转自:https://code.msdn.microsoft.com/Support-Composite-Key-in-d1d53161 he default EntitySetControlle ...
- 如何使用ASP.NET Web API OData在Oracle中使用Entity Framework 6.x Code-First方式开发 OData V4 Service
环境: Visual Studio 2013 + .Net Framework 4.5.2 1.新建项目 2.安装OData,ODP.NET 安装的包: 下面是部分代码: using System; ...
- vs2012 + web api + OData + EF + MYsql 开发及部署
先说下我的情况,b/s开发这块已经很久没有搞了,什么web api .MVC.OData都只是听过,没有实际开发过,因为最近要开发一个手机app的服务端,所以准备用这套框架来开发. 下面开始进入正题( ...
- [转]vs2012 + web api + OData + EF + MYsql 开发及部署
本文转自:http://www.cnblogs.com/liumang/p/4403436.html 先说下我的情况,b/s开发这块已经很久没有搞了,什么web api .MVC.OData都只是听过 ...
随机推荐
- pageadmin CMS 如何添加自定义页面
理论上网站上的所有页面都可以通过栏目管理来添加,那自定义页面的意义是什么呢? 网站的需求是很多样化的,比如需要制作一个对外提供数据的api,甚至制作一个搜索页面,或者制作一些数据和栏目没有对应关系的页 ...
- Kubernetes 集群安装部署
etcd集群配置 master节点配置 1.安装kubernetes etcd [root@k8s ~]# yum -y install kubernetes-master etcd 2.配置 etc ...
- gitlab中修改项目名称客户端修改方法
如果gitlab项目名称已经修改,对于本地已经克隆下来的仓库,可以使用如下命令进行修改: git remote set-url origin 新的项目路径
- PHP header函数设置http报文头示例详解以及解决http返回头中content-length与Transfer-Encoding: chunked的问题
最近在服务器上,多媒体与设备(摄像头)对接的时候,总是发生错误导致设备崩溃,抓包发现响应头不对,没有返回length,使得摄像头立即崩溃.找了一下资料,改了一下响应头就好了. //定义编码 heade ...
- nosql基本了解
数据库分为关系型数据库和非关系型数据库nosql,关系型数据库比较常见,此处不再多讲:nosql有key-value存储数据库,比如redis:文档型数据库,比如mongodb:列存储数据库,比如hb ...
- 【vue】——使用watch 观察路由变化,重新获取内容
更新:11-29 时隔半年,又重新使用VUE进行开发,有了新方案--beforeRouteLeave 在组件内直接使用,前提是你用了vue-router: beforeRouteLeave (to, ...
- 个人KPI制定
1.工作量 1.1 能独立完成工作优先级 1.2 能独立预估工作时间 2.工作质量 2.1 项目按时完成没有延期 2.2 交付件质量 2.2.1 测试用例设计没有明显遗漏 2.2.2 测试bug符合规 ...
- 40.oracle事务
一.事务特性 事务必须具备以下四个特性,简称ACID属性 原子性(Atomicity):事务是一个完整的操作.事务的各步操作是不可分割的(原子的):要么都执行,要么都不执行场景:银行转账 A-100 ...
- 【转载】MDX Step by Step 读书笔记(三) - Understanding Tuples (理解元组)
1. 在 Analysis Service 分析服务中,Cube (多维数据集) 是以一个多维数据空间来呈现的.在Cube 中,每一个纬度的属性层次结构都形成了一个轴.沿着这个轴,在属性层次结构上的每 ...
- Visual Studio 跨平台開發實戰(4) - Xamarin Android 基本控制項介紹 (转帖)
前言 不同於iOS, Xamarin 在Visual Studio中針對Android, 可以直接設計使用者介面. 在本篇教學文章中, 筆者會針對Android的專案目錄結構以及基本控制項進行介紹, ...