This post is part 4 of the Web API and OData V4 series. This article shows how Web API 2.2 with OData V4 supports enum in entities or as return values and also supports enum parameters for 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 3 Web 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/WebAPIODataV4
To demonstrate OData with enums, an PhoneNumberTypeEnum enum is created and also an entity class with the string property ‘Name’ as the OData key.
using System; using System.ComponentModel; using System.ComponentModel.DataAnnotations; namespace WebAppODataV4.Models { public enum PhoneNumberTypeEnum { Cell = 1, Home = 2, Work = 3 } public class EntityWithEnum { public string Description { get; set; } public PhoneNumberTypeEnum PhoneNumberType { get; set; } [Key] public string Name { get; set; } } }
Using Entities which have enum Properties
The EntityWithEnum is added to the OData model builder.
builder.EntitySet<EntityWithEnum>("EntityWithEnum");
An EntityWithEnumController which implements ODataController is used to make the OData entity set available in the service. Some dummy data has been added in the contructor.
[ODataRoutePrefix("EntityWithEnum")] public class EntityWithEnumController : ODataController { private List<EntityWithEnum> someData = new List<EntityWithEnum>(); public EntityWithEnumController() { someData.Add(new EntityWithEnum { Description = "test1", Name = "Van", PhoneNumberType = PhoneNumberTypeEnum.Home}); someData.Add(new EntityWithEnum { Description = "test2", Name = "Bill", PhoneNumberType = PhoneNumberTypeEnum.Work }); someData.Add(new EntityWithEnum { Description = "test3", Name = "Rob", PhoneNumberType = PhoneNumberTypeEnum.Cell }); } [ODataRoute] [EnableQuery(PageSize = 20)] public IHttpActionResult Get() { return Ok(someData); } [ODataRoute] [EnableQuery(PageSize = 20)] public IHttpActionResult Get([FromODataUri] string key) { if (someData.Exists(t => t.Name == key)) { return Ok(someData.FirstOrDefault(t => t.Name == key)); } return BadRequest("key does not key"); } }
Now an entity can be requested with a string key.
http://localhost:51902/odata/EntityWithEnum('Bob')
The enum is serialized and returned in the Json object.
{ "@odata.context":"http://localhost:51902/odata/$metadata#EntityWithEnum/$entity","Description":"test1","PhoneNumberType":"Home","Name":"Van" }
Using enums in OData Functions
OData functions support using enum as parameters or return values. To demonstrate this, a new OData function is created, which takes a PhoneNumberTypeEnum enum parameter and returns an enum parameter.
EntitySetConfiguration<EntityWithEnum> entitesWithEnum = builder.EntitySet<EntityWithEnum>("EntityWithEnum"); FunctionConfiguration functionEntitesWithEnum = entitesWithEnum.EntityType.Collection.Function("PersonSearchPerPhoneType"); functionEntitesWithEnum.Parameter<PhoneNumberTypeEnum>("PhoneNumberTypeEnum"); functionEntitesWithEnum.ReturnsCollectionFromEntitySet<EntityWithEnum>("EntityWithEnum");
The function method is added to the ODataController.
[HttpGet] [EnableQuery(PageSize = 20, AllowedQueryOptions = AllowedQueryOptions.All)] [ODataRoute("Default.PersonSearchPerPhoneType(PhoneNumberTypeEnum={phoneNumberTypeEnum})")] public IHttpActionResult PersonSearchPerPhoneType([FromODataUri] PhoneNumberTypeEnum phoneNumberTypeEnum) { if (!ModelState.IsValid) { return BadRequest(ModelState); } return Ok(someData.Where(t => t.PhoneNumberType.Equals(phoneNumberTypeEnum))); }
The function can be called with a Http Get request. The enum is entered as a parameter; the enum property with full namespace.
http://localhost:51902/odata/EntityWithEnum/Default.PersonSearchPerPhoneType(PhoneNumberTypeEnum=WebAppODataV4.Models.PhoneNumberTypeEnum'Cell')
The request returns a Json object with the enum value.
{ "@odata.context":"http://localhost:51902/odata/$metadata#EntityWithEnum","value":[ { "Description":"test3","PhoneNumberType":"Cell","Name":"Rob" } ] }
Note: At present an enum cannot be used as a parameter for an action.
Links:
http://msdn.microsoft.com/en-us/library/ff478141.aspx
http://www.odata.org/documentation/odata-version-2-0/uri-conventions/
SAMPLES: https://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/OData/v4/
https://aspnetwebstack.codeplex.com/SourceControl/latest
http://aspnetwebstack.codeplex.com/
http://www.asp.net/web-api/overview/releases/whats-new-in-aspnet-web-api-22
http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-routing-conventions
http://stackoverflow.com/questions/18233059/apicontroller-vs-odatacontroller-when-exposing-dtos
http://meyerweb.com/eric/tools/dencoder/
http://techbrij.com/jquery-datatables-asp-net-web-api-odata-service
http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options
http://www.dotnetcurry.com/showarticle.aspx?ID=1008
Reblogged this on Dinesh Ram Kali..
Thank you very much!
But how can we return enum’s integer value, not string?