You do Not Create REST Service Using ASP.Net Web API

There was a time when developers would create Web Services using the WCF. Their argument was that using basicHttpBinding makes a WCF service a Web Service. Yes that ASMX based web service. I never agreed to this argument.

basicHttpBinding

Clearly WCF was an evolution from ASMX Web Service. Now let us talk about the purpose of this article, that saga of Web API and REST Service. To start with let us understand the background.

Rest Service

REST Service

A typical REST Service in WCF would look such as follows:

EndPoint for REST Service

The point to be noticed here is that when creating the service we are defining the resource type. Since in the preceding service we want to work with both a JSON and XML message format, we are creating two services for it.

An EndPoint for a REST Service can be created using the factor as shown in the snippet below.

ApiController
This is the simplest WCF REST Service one could create. We can have multiple HTTP operations on the same URL. We can do HTTP GET, POST, PUT and DELETE on the same resource URL in the WCF REST service. However when creating the service, we need to take care of the message format. A WCF REST service uses the WCF channel and messaging architecture that is used in the WCF SOAP service.

ASP.NET Web API

This is based on the ASP.NET MVC architecture to create an API for the web. We can create a RESTful API using the ASP.NET Web API. A simple Web API is a class that inherits ApiController class.

class

Keep in mind that ASP.NET Web APIs do not have the EndPoints and bindings. It utilizes the ASP.NET MVC routings to resolve the resources. Here resource describes their own capabilities and interactions.

To understand the difference between a REST Service and ASP.NET Web API, let us try to understand The Richardson Maturity Model.

RMM: Richardson Maturity Model classify an API into various levels on the basis of how well they use or take advantage of the web.

Richardson Maturity Model

There are four levels in the RMM from level 0 to level 3.

RMM

Now we know the various levels of RMM, so let us try to map WCF REST Service and the ASP.NET Web API to RMM.

A WCF REST Service:

  1. Can have multiple URIs.
  2. Can have multiple HTTP methods.
  3. Operations are defined by the URI.
  4. Each operation has its own URI.
  5. Each URI cross ponds to a method on the server.

Let us examine the nature of REST Service. Let us assume we want to do CRUD operations on the BloodDonor table. A WCF REST Service allows us to have multiple methods on the same URI. So it is very much resource oriented. The resource is uniquely identified by the URI and we can have multiple HTTP methods on the same resource or URI. A WCF REST Service for a CRUD operation may look such as in the following listing:

CRUD operation

Before we conclude that a WCF REST Service can be placed in Level 2, there is a catch in it. The WCF REST created above does not know the content negotiation. The client cannot request a specific type of content and can only work with the XML message format (in this case). A WCF REST service cannot negotiate with the resource on the same URI in multiple message formats.

WCF REST Service

Now let us focus on the ASP.NET Web API. It can be also be placed in Level 2 with content negotiation and the support for multiple message formats on the same URI.

multiple message format

A typical ASP.NET Web API may look as shown in the following listing:

Web API

The ASP.NET Web API has a single URI for multiple operations working on multiple HTTP methods. However unlike WCF REST, while accessing ASP.NET Web API, the client can negotiate the content. The client can pass the message format it wants to work with.

Let us see this in action using Fiddler. In Fiddler, when we pass a content type as XML, the ASP.NET Web API will return a response in XML message format as shown in the following image:

content type

In Fiddler, when we pass a content type as JSON, the ASP.NET Web API will return a response in JSON message format as in the following image:

request header

Clearly in the ASP.NET Web API, the client can pass the message format in the request header. Some other characteristics of the ASP.NET Web API over the WCF REST Service are as follows:

  • Unit Testability
  • Support for multiple format.
  • Symmetric programming experience at client and server.
  • Multiple hosting options.

By discussion so far we can say you don't create a REST Service using the Web API. The Web API has more features to put itself in the Level 2 of the Richardson Maturity Model.

REST Service using the Web API

I am sure this is a subjective discussion, so I would love to read your thoughts on that. Feel free to provide your view on my understanding.
Happy coding.

Up Next
    Ebook Download
    View all
    Learn
    View all