Web API is a popular tool used for current middle-tier development. Web API is based on the concept of REST. These two articles are summaries of REST and REST-related issues with my understanding.
The second article was my learning notes several years ago, summarising the REST features, related issues and my understanding. When I tried to rewrite them into an article, I found out that I should review and briefly summarize the basic concept of REST, but that is not just easy work and can be done in several words or sentences. Then I think I'd better write another article for the basic concept of REST, that is
REST(1), In Concept.
This article will be emphasizing on REST in practice, with more of my understanding.
Introduction
This will be the contents of this article,
- A - What is REST
- B - Principles of REST
- C - REST Based Service vs. RESTful Service
- ASP.NET MVC vs. Web API
- AJAX vs. Web API
- WCF REST vs. Web API
A: What is REST
Theoretically, REST (REpresentational State Transfer) is basically an architectural style that could be implemented by any languages or any platform, while ASP.NET Web API is a Microsoft tool to produce the RESTful output through HTTP for Clients (RESTful is typically used to refer to web services implementing such a REST architecture. ).
However, as a Microsoft developer, we could think ASP.NET Web API is a REST model, and according to it to understand the features of REST.
B: Principles of REST[ref]
REST (REpresentational State Transfer) is basically an architectural style of development having some principles...
- It should be stateless
- It should access all the resources from the server using only URI
- It does not have inbuilt encryption
- It does not have a session
- It uses one and only one protocol is HTTP
- For performing CRUD operations, it should use HTTP verbs such as to get, post, put and delete
- It should return the result only in the form of JSON or XML, atom, OData, etc. (lightweight data )
C: REST Based Service vs. RESTful Service[ref]
- RESTFUL services mean it follows all the above principles.
- REST-based services follow some of the above principles and not all
It is similar to the concept of,
- Object-oriented languages support all of the OOPs features, for examples: C++, C#
- Object-based languages support some of the OOPs concepts, for examples: JavaScript, VB
1. ASP.NET MVC vs. Web API[
ref]
ASP.NET MVC 4 is REST-Based while Microsoft WEB API is RESTFul.
MVC supports only some of the above REST principles whereas WEB API supports all the above REST Principles.
MVC only supports the following from the REST API,
- We can access the resource using URI
- It supports the HTTP verb to access the resource from the server
- It can return the results in the form of JSON, XML, that is the HTTPResponse.
However, at the same time in MVC,
- We can use the session
- We can make it stateful
- We can return video or image from the controller action method which basically violates the REST principles
That is why MVC is REST-Based whereas WEB API supports all the above principles and is RESTFul.
AJAX is REST-Based while Microsoft WEB API is RESTFul.
High REST - Uses all 4 verbs and uses well-constructed URIs.
LOW REST - Uses mostly Get and POST verbs only. More suited for Ajax services.
NOTE
PUT and DELETE verbs are not supported by all browsers. Also, PUT and DELETE can be handled using the POST verb itself.
HTTP Verbs
HTTP defines nine methods (sometimes referred to as "verbs") indicating the desired action to be performed on the identified resource.
- GET GET method requests a representation of the specified resource. Requests using GET should only retrieve data.
- POST POST method is used to submit an entity to the specified resource, often causing a change in state or side effects on the server.
- PUT PUT method replaces all current representations of the target resource with the request payload.
- DELETE DELETE method deletes the specified resource.
- PATCH PATCH method is used to apply partial modifications to a resource.
- HEAD HEAD method asks for a response identical to that of a GET request, but without the response body.
- CONNECT CONNECT method establishes a tunnel to the server identified by the target resource.
- OPTIONS OPTIONS method is used to describe the communication options for the target resource.
- TRACE TRACE method performs a message loop-back test along the path to the target resource.
while the most popular 4 are
3. WCF REST vs. Web API[
ref]
WCF REST is REST-Based while Microsoft WEB API is RESTFul.
WCF Rest
- To use WCF as a WCF Rest service you have to enablewebHttpBindings.
- It supports HTTP GET and POST verbs by [WebGet] and [WebInvoke] attributes respectively.
- To enable other HTTP verbs you have to do some configuration in IIS to accept the request of that particular verb on .svc files
- Passing data through parameters using a WebGet needs configuration. The UriTemplate must be specified.
- It supports XML, JSON, and ATOM data format.
Summary
This article is understanding the concept of REST and RESTful Service in practice.
Reference