Skip to main content

Restful Web Services Through ASP.Net Web API 2

May 27, 2014

In this blog, I’ll discuss ASP.Net Web API 2, plus the architecture and details of using it to work with data.

ASP.Net Web API 2 – An Introduction

ASP.NET Web API 2 is a framework for building Restful web services that can be accessed through the internet using the HyperText Transfer Protocol (HTTP).  Client applications like browsers, mobile and desktop devices can consume and upload data through these services using standard HTTP methods including GET, PUT, POST and DELETE.

ASP.Net Web API 2 is a Microsoft server side technology; however, any client technology that can perform web requests and can consume data through the Web API may be used.

The Web API 2 web services may be hosted in Internet Information Services (IIS) or self-hosted as a service or console application, typically under the Windows Server operating system.

The Web API 2 web services are an alternative and newer technology than web services created using the Microsoft Windows Communication Framework (WCF).  Web API 2 is the preferred technology to use when creating HTTP-based services that need to be accessible from the widest variety of clients.


The architecture for a complete application includes the server side components and exposed Restful services, and clients.

Server Side Components

Server side components of interest include the Data Access Layer, Controllers and the WebApiConfig.cs file.

Data Access Layer

The data source for the application is typically a relational database, but could be anything from a document database to data stored in an XML or JSON-formatted file.

For simplicity purposes, the Entity Frameworks architecture is used to map database tables and stored procedures through a dbContext object.

The dbContext object is typically set up by the creation of an ADO.Net Entity Data Model.

The following are model classes used for the examples below:

BusinessInfo  Class


BusinessInformation_Result Class



Access to the Web API services is through controllers, distinct modules of code providing methods mapping to Restful web requests.  Controllers are usually mapped to one or more tables and/or related stored procedures. Each controller usually contains the following elements:

  • Creation of a database context
  • Get method to select all objects of a certain type
  • Get method to select a specific object
  • Put method to update data
  • Post method to add data
  • Delete method to delete data
  • 0 or more custom methods to call stored procedures

An example controller is shown below:







The WebAPIConfig.cs file is used to map routes, i.e. web requests to the framework, and to set up allowable output formats.  It consists of the following sections:

  • Web API configuration
  • Standard Routes
  • Custom Routes for action methods
  • Setting up allowable output formats

An example WebApiConfig.cs file is shown below:


Consuming Data at the Client

The following code is used to make each request described in the “Controllers” section above. The following code is written in C#, but other technologies like Java can be used.



The configuration of the allowable web request output format is discussed in the “WebApiConfig.cs” section above.  In this example, all data will be returned in JSON format.

Below is an example of the JSON output from calling the GetBusinessInformation Action method above.

{“BusinessInformation”:[{“UserID”:9,”FirstName”:”paul”,”LastName”:”casey”,”Street”:”Test1″,”City”:”Test City1″,”Phone”:”952-239-0000″,”OrderID”:7,”ItemID”:1,”ItemName”:”Meat Lovers Pizza”,”BaseCost”:12.0000},{“UserID”:9,”FirstName”:”paul”,”LastName”:”casey”,”Street”:”Test1″,”City”:”Test City1″,”Phone”:”952-239-0000″,”OrderID”:8,”ItemID”:5,”ItemName”:”Cheesy Bread”,”BaseCost”:8.9900},{“UserID”:9,”FirstName”:”paul”,”LastName”:”casey”,”Street”:”Test1″,”City”:”Test City1″,”Phone”:”952-239-0000″,”OrderID”:9,”ItemID”:6,”ItemName”:”Hot Wings”,”BaseCost”:8.9900},{“UserID”:9,”FirstName”:”paul”,”LastName”:”casey”,”Street”:”Test1″,”City”:”Test City1″,”Phone”:”952-239-0000″,”OrderID”:9,”ItemID”:5,”ItemName”:”Cheesy Bread”,”BaseCost”:8.9900},{“UserID”:9,”FirstName”:”paul”,”LastName”:”casey”,”Street”:”Test1″,”City”:”Test City1″,”Phone”:”952-239-0000″,”OrderID”:11,”ItemID”:5,”ItemName”:”Cheesy Bread”,”BaseCost”:8.9900},{“UserID”:9,”FirstName”:”paul”,”LastName”:”casey”,”Street”:”Test1″,”City”:”Test City1″,”Phone”:”952-239-0000″,”OrderID”:17,”ItemID”:1,”ItemName”:”Meat Lovers Pizza”,”BaseCost”:12.0000},{“UserID”:9,”FirstName”:”paul”,”LastName”:”casey”,”Street”:”Test1″,”City”:”Test City1″,”Phone”:”952-239-0000″,”OrderID”:18,”ItemID”:1,”ItemName”:”Meat Lovers Pizza”,”BaseCost”:12.0000},{“UserID”:9,”FirstName”:”paul”,”LastName”:”casey”,”Street”:”Test1″,”City”:”Test City1″,”Phone”:”952-239-0000″,”OrderID”:18,”ItemID”:3,”ItemName”:”Hawaiian Pizza”,”BaseCost”:12.9900},{“UserID”:9,”FirstName”:”paul”,”LastName”:”casey”,”Street”:”Test1″,”City”:”Test City1″,”Phone”:”952-239-0000″,”OrderID”:19,”ItemID”:1,”ItemName”:”Meat Lovers Pizza”,”BaseCost”:12.0000},{“UserID”:9,”FirstName”:”paul”,”LastName”:”casey”,”Street”:”Test1″,”City”:”Test City1″,”Phone”:”952-239-0000″,”OrderID”:20,”ItemID”:12,”ItemName”:”pickle”,”BaseCost”:2.9900}]}


ASP.Net Web API 2 provides a framework for building Restful web services that can be accessed through the internet using the HyperText Transfer Protocol (HTTP).  With your introduction to ASP.Net Web API 2 technology plus the architecture and details for building and using it, you’ve got a solid foundation of knowledge to begin.