In this article, we will learn about ASP.NET Web API and why we need it. Most developers think about using Web API but they don't get proper information about that. This is why I have decided to write a complete guide on ASP.NET Web API. If you want to learn all about REST Web API, you should stay with me to learn all the parts of this article.
- What is Web Service ?
- What is WCF ?
- What is REST ?
- Difference between SOAP and REST based services.
- Difference between Web Service, WCF, and Web API.
- Introduction to Web API and why Web API became popular.
What are Web Services (.asmx)?
Before Web Services, remoting was used but because of so many cons of remoting, Web Services replaced it. "Web Service is like a web based application which is normally a class containing methods that could be used by other web based or Windows based applications. It also follows a code-behind layer such as the web pages, although its not containing a user interface."
Web Services is a way to communicate between two different or same web based applications using "webmethod". Web services can be accessible from web based or Windows based applications. Web Services is soap based and transforms data via XML.
For more details about Web Services, you can go through an article written by
Vithal Wadje.
Drawback of Web Services, SOAP in ASP.NET
- Hosting dependency
- Protocol dependency
- Serialization dependency
- Data transformation
And also, there are more drawbacks which were resolved by WCF and Web API. For more details, you can go through this
link.
What is WCF (.svc) and why replace Web Services with WCF ?
Preceding WCF, a .NET 2.0 web reference was made in the clients that went about as an intermediary to encourage the correspondence with a Web Service. .NET 3.5 acquainted another instrument with making these intermediaries that are likewise perfect with WCF Server-side technologies.
"Windows Communication Foundation (Code named Indigo) is a programming stage and runtime framework for building, arranging and conveying system appropriated administrations. It is the most recent administration arranged innovation; Interoperability is the major qualities of WCF. It is bound together programming model gave in .Net Framework 3.0. WCF is a joined element of Web Service, Remoting, MSMQ and COM+. WCF gives a typical stage to all .NET correspondence. you can transfer information as asynchronous messages from one endpoint to another endpoint."
For more details about WCF, go through the given
link.
What is REST ?
REST stands for Representational State Transfer. (Sometimes, we can write it as "ReST".) It's a stateless, client-server, cacheable based protocol to communicate -- and in virtually all points, the HTTP protocol is used. REST is an architecture style for designing networked applications.
Image source - http://b2evolution.net/man/advanced-topics/external-apis/rest-api/
Actually, by using REST, we can create Web API with multiple verbs, e.g GET, POST, PUT, DELETE, and the endpoint will look like this.
GET /GetData?id=2
POST /SaveData?id=2
PUT /UpdateData?id=1052&review_id=2
DELETE /DeleteDataByID?id=2
For more details about REST, go through the given
link.
Difference between SOAP and REST based services
- REST supports multiple verbs over HTTPS (GET,POST,PUT,DELETE)
- SOAP is XML based protocol whereas REST is architectural based.
- In SOAP, WDSL is used for communication between client and server whereas REST uses XML or JSON.
- REST is called by direct URL instead of RPC method.
- Web Services does not return human readable data whereas REST returns JSON data.
- REST is easy to consume whereas SOAP is somewhat complicated.
- REST is 5 times faster than SOAP.
- REST transfers data via HTTP.
- REST is stateless and allows to cache the data.
Difference between Web Service, WCF, and Web API.
Now, I am going to explain the differences between Web Services, WCF, and Web API. Actually, I am trying to explain about scope of the latest technologies.
- Web Services is SOAP based and returns XML data and WCF is also same but Web API returns JSON data.
- Web Services supports only HTTP protocol whereas WCF supports multiple (TCP, named pipe, p2p,etc..) and Web API can be used from any where.
- Web Services can be hosted on IIS while WCF can be hosted anywhere but Web API can be hosted only on IIS and self.
- We can use self hosting in WCF and Web API but it is not allowed in Web Services.
For more details, you can go through the given
links.
Introduction to Web API and why did Web API become popular ?
Web API stands for "
Application programming interface" and it's an open source and rich framework of ASP.NET. ASP.NET Web API follows MVC pattern architecture and has the elements like controller, routing, action, filter, model binder, etc. The Web API area is so broad because it can be consumed from anywhere, at any platform, by any browser. It's totally independent and can be accessed from Mobile, Tablet, Android, Windows etc.
According to this image, we can easily understand that Web API can be consumed from anywhere.
- It supports CRUD based action with HTTP verbs GET, POST, PUT and DELETE.
- Response with status code.
- Web API supports MediaTypeFormatter like multipart/formdata.
- Easy to create and consume.
I hope, you have learned why we should use Web API over Web Services or WPF. Also, we discussed some drawbacks of Web Services and WCF.
In this article, I have written only the introductory part of Web API. For more about Web API, wait for part 2.