uid | title | author | description | ms.author | ms.date | ms.assetid | msc.legacyurl | msc.type |
---|---|---|---|---|---|---|---|---|
web-api/overview/odata-support-in-aspnet-web-api/odata-v4/using-a-singleton-in-an-odata-endpoint-in-web-api-22 |
Create a Singleton in OData v4 Using Web API 2.2 | Microsoft Docs |
rick-anderson |
This topic shows how to define a singleton in an OData endpoint in Web API 2.2. |
riande |
06/27/2014 |
4064ab14-26ee-4d5c-ae58-1bdda525ad06 |
/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/using-a-singleton-in-an-odata-endpoint-in-web-api-22 |
authoredcontent |
by Zoe Luo
Traditionally, an entity could only be accessed if it were encapsulated inside an entity set. But OData v4 provides two additional options, Singleton and Containment, both of which WebAPI 2.2 supports.
This article shows how to define a singleton in an OData endpoint in Web API 2.2. For information on what a singleton is and how you can benefit from using it, see Using a singleton to define your special entity. To create an OData V4 endpoint in Web API, see Create an OData v4 Endpoint Using ASP.NET Web API 2.2.
We'll create a singleton in your Web API project using the following data model:
A singleton named Umbrella
will be defined based on type Company
, and an entity set named Employees
will be defined based on type Employee
.
-
Define the CLR types.
[!code-csharpMain]
-
Generate the EDM model based on the CLR types.
[!code-csharpMain]
Here,
builder.Singleton<Company>("Umbrella")
tells the model builder to create a singleton namedUmbrella
in the EDM model.The generated metadata will look like the following:
[!code-xmlMain]
From the metadata we can see that the navigation property
Company
in theEmployees
entity set is bound to the singletonUmbrella
. The binding is done automatically byODataConventionModelBuilder
, since onlyUmbrella
has theCompany
type. If there is any ambiguity in the model, you can useHasSingletonBinding
to explicitly bind a navigation property to a singleton;HasSingletonBinding
has the same effect as using theSingleton
attribute in the CLR type definition:[!code-csharpMain]
Like the EntitySet controller, the singleton controller inherits from ODataController
, and the singleton controller name should be [singletonName]Controller
.
[!code-csharpMain]
In order to handle different kinds of requests, actions are required to be pre-defined in the controller. Attribute routing is enabled by default in WebApi 2.2. For example, to define an action to handle querying Revenue
from Company
using attribute routing, use the following:
[!code-csharpMain]
If you are not willing to define attributes for each action, just define your actions following OData Routing Conventions. Since a key is not required for querying a singleton, the actions defined in the singleton controller are slightly different from actions defined in the entityset controller.
For reference, method signatures for every action definition in the singleton controller are listed below.
[!code-csharpMain]
Basically, this is all you need to do on the service side. The sample project contains all of the code for the solution and the OData client that shows how to use the singleton. The client is built by following the steps in Create an OData v4 Client App.
.
Thanks to Leo Hu for the original content of this article.