OData Feed Using ASP.Net Web API

Introduction

In this article, you will see how to use OData with the ASP. NET Web API.

This article describes the creation of an OData feed through the ASP.NET Web API. Here we use the MVC application but you can use the web Forms application. The assumption is made that the application uses the .NET 4.5 Framework. If we use the Web Forms application then we need to create the two sub folders:

  • The first one is named "Controller"
  • The second one is named "Model"
  • And follow the same procedure from the Step 2.

Now I describe the procedure for creating this application.

Step 1

First we need to create the MVC4 Web API application.

  • Start Visual Studio 2012.
  • Select "File"->"New"->"Project".
  • On the template window select "Installed" -> "Template" -> "Other Languages" -> "Visual C#" -> "Web".
  • Now choose "ASP.NET MVC4 Web Application" and change the Name to "WebApiOData".
  • Click on the "OK" button.

    oData2.jpg

Now the New ASP.NET MVC4 Project window is opened then:

  • In this window select the "Web API".
  • And click on the "OK" button.

    odata3.jpg

Step 2

Now we create the Controller class using the following:

  • In the Solution Explorer.
  • Right-click on the "Controller folder" -> "Add" -> "New Item".

    odata4.jpg

  • On the template window select "Installed" -> "Visual C#" -> Web.
  • Choose "Web API Controller Class" and change the name to "MeetingsController".
  • Click on the "Add" button.

    odata5.jpg

In this Controller we add the following code.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Web.Http;  
  6. using System.Web.Http.OData.Query;  
  7. using WebApiOData.Models;  
  8. namespace WebApiOData.Controllers  
  9. {  
  10.     public class MeetingsController : ApiController  
  11.     {  
  12.         private readonly IList<Meeting> _scheduledMeetings;  
  13.         public MeetingsController()  
  14.         {  
  15.             _scheduledMeetings = new List<Meeting>  
  16.             {  
  17.                 new Meeting { Id = "1", Leader = "Jhon ", MeetingDate = new DateTime(2013, 5, 16), Title = "Project discussion" },  
  18.                 new Meeting { Id = "3", Leader = "Henary", MeetingDate = new DateTime(2013, 5, 27), Title = "project Planning" },  
  19.                 new Meeting { Id = "6", Leader = "Smith", MeetingDate = new DateTime(2013, 6, 1), Title = "Advanced  Technology" },  
  20.                 new Meeting { Id = "7", Leader = "Robbin hudda", MeetingDate = new DateTime(2013, 6, 17), Title = "Maintenance" },  
  21.                 new Meeting { Id = "9", Leader = "Carter", MeetingDate = new DateTime(2013, 6, 27), Title = "Company Forum" },  
  22.                 new Meeting { Id = "10", Leader = "Rollin Hand", MeetingDate = new DateTime(2013, 5, 21), Title = "Selling product" }  
  23.             };  
  24.         }  
  25.         // GET api/Meeting  
  26.         [Queryable(AllowedQueryOptions = AllowedQueryOptions.All)]  
  27.         public   IQueryable<Meeting> Get()  
  28.         {  
  29.             return _scheduledMeetings.AsQueryable();  
  30.         }  
  31.         // POST api/<controller>  
  32.         public void Post([FromBody]string value)  
  33.         {  
  34.         }  
  35.         // PUT api/<controller>/5  
  36.         public void Put(int id, [FromBody]string value)  
  37.         {  
  38.         }  
  39.         // DELETE api/<controller>/5  
  40.         public void Delete(int id)  
  41.         {  
  42.         }  
  43.     }  
  44. }   

Step 3

Now we add the "Class"  using the following:

  • In the Solution Explorer.
  • Right-click on the "Controller folder" -> "Add" -> "Class".

    odata6.jpg

  • On the template window select "Installed" -> "Visual C#".
  • Choose "Class" and change the name to "Meeting".
  • Click on the "Add" button.

    odata7.jpg

Now add the following code in this class.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace WebApiOData.Models  
  6. {  
  7.     public class Meeting  
  8.     {  
  9.         public string Id { getset; }  
  10.         public DateTime MeetingDate { getset; }  
  11.         public string Title { getset; }  
  12.         public string Leader { getset; }  
  13.     }  
  14. } 

Step 4

Now we need to install "ASP.NET Web API OData" using the following:

  • In the Solution Explorer.
  • Right-click on the project then select "WebApiOData" -> "Manage Nuget Packages".
  • Open a Nuget window then select Online.
  • And in the search box enter "ASP.NET Web API OData".
  • Install the"Microsoft ASP.NET Web API OData".

    odata.jpg

    odata1.jpg
Click on the "I Accept" button for installing the Microsoft ASP.NET Web API OData.

Step 5

Open the "Global. asax" file from the Solution Explorer.

odata12.jpg

And add the following code in the "Application Start":

  1. GlobalConfiguration.Configuration.Routes.MapHttpRoute(  
  2.         name: "ODataRestApi",  
  3.         routeTemplate: "api/v1/{controller}"  
  4.     ); AreaRegistration.RegisterAllAreas(); 

Step 6

Formatting the output

This is used for controlling the data format coming back through the optional OData "format"=query parameter. It is not necessary for the query operation. We provide the option of XML and JSON in the jQuery.

We add the following code into the Global.asax file at the right before the call to the MapHttpRoute.

  1. GlobalConfiguration.Configuration.Formatters.JsonFormatter.AddQueryStringMapping("$format""json""application/json");  
  2. GlobalConfiguration.Configuration.Formatters.XmlFormatter.AddQueryStringMapping("$format""xml""application/xml");     

 

And we need to add the following namespace:

  1. using System.Net.Http.Formatting;

All the code of the Global.asax file:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Http;  
  6. using System.Web.Mvc;  
  7. using System.Web.Optimization;  
  8. using System.Web.Routing;  
  9. using System.Net.Http.Formatting;  
  10. namespace WebApiOData  
  11. {  
  12.     // Note: For instructions on enabling IIS6 or IIS7 classic mode,  
  13.     // visit http://go.microsoft.com/?LinkId=9394801  
  14.     public class WebApiApplication : System.Web.HttpApplication  
  15.     {  
  16.         protected void Application_Start()  
  17.         {  
  18.             GlobalConfiguration.Configuration.Formatters.JsonFormatter.AddQueryStringMapping("$format""json""application/json");
  19.             GlobalConfiguration.Configuration.Formatters.XmlFormatter.AddQueryStringMapping("$format""xml""application/xml");  
  20.             GlobalConfiguration.Configuration.Routes.MapHttpRoute(  
  21.         name: "ODataRestApi",  
  22.         routeTemplate: "api/v1/{controller}"  
  23.     ); AreaRegistration.RegisterAllAreas();  
  24.             WebApiConfig.Register(GlobalConfiguration.Configuration);  
  25.             FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);  
  26.             RouteConfig.RegisterRoutes(RouteTable.Routes);  
  27.             BundleConfig.RegisterBundles(BundleTable.Bundles);  
  28.         }  
  29.     }  
  30. }  

Step 7

Now we execute the application.

Output for top 2 in XML form:

http://localhost:36137/api/v1/meetings?$top=2&$format=xml

odata9.jpg


Output for top2 in JSON format:

http://localhost:36137/api/v1/meetings?$top=2&$format=json

odata11.jpg


odata10.jpg

Output in XML format:

http://localhost:36137/api/v1/meetings

odata8.jpg


Similar Articles