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.
Now the New ASP.NET MVC4 Project window is opened then:
- In this window select the "Web API".
- And click on the "OK" button.
Step 2
Now we create the Controller class using the following:
- In the Solution Explorer.
- Right-click on the "Controller folder" -> "Add" -> "New Item".
- 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.
In this Controller we add the following code.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Web.Http;
- using System.Web.Http.OData.Query;
- using WebApiOData.Models;
- namespace WebApiOData.Controllers
- {
- public class MeetingsController : ApiController
- {
- private readonly IList<Meeting> _scheduledMeetings;
- public MeetingsController()
- {
- _scheduledMeetings = new List<Meeting>
- {
- new Meeting { Id = "1", Leader = "Jhon ", MeetingDate = new DateTime(2013, 5, 16), Title = "Project discussion" },
- new Meeting { Id = "3", Leader = "Henary", MeetingDate = new DateTime(2013, 5, 27), Title = "project Planning" },
- new Meeting { Id = "6", Leader = "Smith", MeetingDate = new DateTime(2013, 6, 1), Title = "Advanced Technology" },
- new Meeting { Id = "7", Leader = "Robbin hudda", MeetingDate = new DateTime(2013, 6, 17), Title = "Maintenance" },
- new Meeting { Id = "9", Leader = "Carter", MeetingDate = new DateTime(2013, 6, 27), Title = "Company Forum" },
- new Meeting { Id = "10", Leader = "Rollin Hand", MeetingDate = new DateTime(2013, 5, 21), Title = "Selling product" }
- };
- }
-
- [Queryable(AllowedQueryOptions = AllowedQueryOptions.All)]
- public IQueryable<Meeting> Get()
- {
- return _scheduledMeetings.AsQueryable();
- }
-
- public void Post([FromBody]string value)
- {
- }
-
- public void Put(int id, [FromBody]string value)
- {
- }
-
- public void Delete(int id)
- {
- }
- }
- }
Step 3
Now we add the "Class" using the following:
- In the Solution Explorer.
- Right-click on the "Controller folder" -> "Add" -> "Class".
- On the template window select "Installed" -> "Visual C#".
- Choose "Class" and change the name to "Meeting".
- Click on the "Add" button.
Now add the following code in this class.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace WebApiOData.Models
- {
- public class Meeting
- {
- public string Id { get; set; }
- public DateTime MeetingDate { get; set; }
- public string Title { get; set; }
- public string Leader { get; set; }
- }
- }
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".
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.
And add the following code in the "Application Start":
- GlobalConfiguration.Configuration.Routes.MapHttpRoute(
- name: "ODataRestApi",
- routeTemplate: "api/v1/{controller}"
- ); 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.
- GlobalConfiguration.Configuration.Formatters.JsonFormatter.AddQueryStringMapping("$format", "json", "application/json");
- GlobalConfiguration.Configuration.Formatters.XmlFormatter.AddQueryStringMapping("$format", "xml", "application/xml");
And we need to add the following namespace:
- using System.Net.Http.Formatting;
All the code of the Global.asax file:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Http;
- using System.Web.Mvc;
- using System.Web.Optimization;
- using System.Web.Routing;
- using System.Net.Http.Formatting;
- namespace WebApiOData
- {
-
-
- public class WebApiApplication : System.Web.HttpApplication
- {
- protected void Application_Start()
- {
- GlobalConfiguration.Configuration.Formatters.JsonFormatter.AddQueryStringMapping("$format", "json", "application/json");
- GlobalConfiguration.Configuration.Formatters.XmlFormatter.AddQueryStringMapping("$format", "xml", "application/xml");
- GlobalConfiguration.Configuration.Routes.MapHttpRoute(
- name: "ODataRestApi",
- routeTemplate: "api/v1/{controller}"
- ); AreaRegistration.RegisterAllAreas();
- WebApiConfig.Register(GlobalConfiguration.Configuration);
- FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
- RouteConfig.RegisterRoutes(RouteTable.Routes);
- BundleConfig.RegisterBundles(BundleTable.Bundles);
- }
- }
- }
Step 7
Now we execute the application.
Output for top 2 in XML form:
http://localhost:36137/api/v1/meetings?$top=2&$format=xml
Output for top2 in JSON format:
http://localhost:36137/api/v1/meetings?$top=2&$format=json
Output in XML format:
http://localhost:36137/api/v1/meetings