Scaffolding The Web API 2 OData Controller in Visual Studio 2013

Introduction

In this article I am describing the OData Controller Scaffolding. Microsoft revealed the new platform to scaffold the OData Controller in the Visual Studio 2013. I will scaffold the OData Controller on a Web API2 project.

So let's use an example and follow the sections below:

  • Create an ASP.NET Web Application
  • Scaffolding the OData Controller
  • Examine the Controller

Create an ASP.NET Web Application

Use the following procedure to create a sample web application.

Step 1: Open Visual Studio 2013

Step 2: Click "New Project" to create an application.

Create New Application

Step 3: Select the Project Template and choose the Web API checkbox.

MVC Template with Web API

Step 4: Click "Create Project".

Visual Studio creates the application with the Web API Core Reference.

Scaffolding the OData Controller

Now we will scaffold the OData Controller. To scaffold use the following procedure.

Step 1: In your Solution Explorer, right-click on your Models folder to add a class.

Adding Class in Models

Step 2: Enter the name as "Employee" and replace the boilerplate code with the following code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

 

namespace OData_Controller_App.Models

{

    public class Employee

    {

        public int EmployeeID { get; set; }

        public string EmployeeName { get; set; }

        public string CompanyName { get; set; }

    }

}

Step 3: Build your project

Building Project

Step 4: In your Solution Explorer, right-click on the Controller folder to add the New Scaffolded Item.

Add New Scaffolded Item

Step 5: Select the We API from the left pane and then select the Web API 2 Odata Controller.

Web API2 OData Controller

Step 6: In the next Add Controller wizard, enter the controller name and select the model Class and click on New data context

Adding Model Class in Add Controller Wizard

Step 7: Enter the data context name (Optional).

New Data Context

Step 8: Click on "Add".

Adding Data Context in Add Controller Wizard

It will scaffold the OData Controller and generate the EmployeeController.cs file. It will also generate the readme.txt file to do some instructions.

Readme File

Step 9: Search for the WebApiConfig file in your Solution Explorer and open the file. It will look as shown below:

Web API Config File

Step 10: As mentioned in the readme.txt file, change the code as in the following code:

using System.Web.Http;

using System.Web.Http.OData.Builder;

using OData_Controller_App.Models;

 

namespace OData_Controller_App

{

    public static class WebApiConfig

    {

        public static void Register(HttpConfiguration config)

        {

            // Web API configuration and services

 

            // Web API routes

            config.MapHttpAttributeRoutes();

 

            config.Routes.MapHttpRoute(

                name: "DefaultApi",

                routeTemplate: "api/{controller}/{id}",

                defaults: new { id = RouteParameter.Optional }

            );

 

            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

            builder.EntitySet<Employee>("Employee");

            config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel());

 

        }

    }

}

Examine the Controller

The scaffolded controller has the GET and POST methods and you can review the methods as shown below:

Scaffolded Controller

Summary

This article will help you to work with the New OData Controller in the Web API 2 project in the Visual Studio 2013. You should create the rest instructions directed the readme.txt file to scaffold the controller and generate the view. 


Similar Articles