Introduction to ASP.NET Web API:
In this article, I am going to give you a brief introduction to ASP.NET WEB API Framework. At the end of this article, you will have a very good understanding of the following things
Let’s discuss each of these questions one by one.
What is ASP.NET Web API?
The term API stands for “Application Programming Interface“. ASP.NET Web API is a framework, provided by Microsoft, which makes it easy to build Web APIs, i.e. HTTP based services. The ASP.NET Web API is an ideal platform for building Restful services on the top of the .NET Framework. These Web API services can be consumed by a variety of clients such as
- Mobile applications
- Desktop applications
- IOTs, etc.
The most important thing to keep in mind is that we can develop both Restful and Non-Restful Web Services using the ASP.NET Web API framework. But mostly this framework is used to create RESTful services. In short, this framework does not provide any specific architectural style for creating the services. In this article series, we are going to discuss creating RESTful services from scratch using the Web API framework.
What are the IOTs?
IOTs stands for Internet Of Things and it is actually a network of physical devices, buildings, vehicles, and other items that are embedded with electronics, sensors, software, actuators, and network connectivity that enable these objects to collect and exchange the information or you can say data.
In other words, we can say, the Internet Of Things (IoT) are the objects or devices which should have an IP address and they can communicate with other Internet-enabled devices and objects over the Internet. Examples of IoT include security systems, thermostats, electronic appliances, cars, etc, in addition to laptops, desktops, and smartphones.
What are the RESTful services?
The term REST stands for Representational State Transfer. REST was first introduced in the year 2000. The REST is actually an architectural pattern that is basically used for creating Web API’s which uses HTTP as the communication method. This REST architectural pattern specifies a set of constraints and those constraints a system should follow to be considered as a Restful Service. The following are the REST constraints.
The Client-Server Constraint is the first constraint of Rest Architectural Pattern. This constraint specifies that a Client sends a request to the server and the server sends a response back to the client. This separation of concerns supports the independent development of both client-side logic and server-side logic.
The next constraint of Rest Architectural Pattern is the Stateless Constraint. The Stateless Constraint specifies that the communication between the client and server should be stateless between the requests. That means we should not be storing any information about the client on the server. The request that comes from the client should contain all the necessary information that is required for the server to process that request which will ensure that each request coming from the client will be treated independently by the server.
The next Constraint is Cacheable Constraint. Some of the master data provided by the server like a list of countries, or list of cities or list of the department in a company does not change that often. The Cacheable Constraint states that let the client decide how long this data is good for so that the client does not need to come back to the server again and again for the same data.
The Uniform Interface constraint defines an interface between the client and the server. To understand the uniform interface constraint, we need to understand what a resource is and the HTTP verbs – GET, PUT, POST and DELETE. From the REST API Point of view, the resources typically represent the data entities such as Product, Student, Customer, etc. The HTTP verb such as GET, POST, PUT, and DELETE which is sent with each request from the client tells the API what to do with the resource. Each resource in a REST API is identified by a unique URI (Uniform Resource Identifier). The following table shows some typical requests that you see in an API
- /Students if the verb is GET here it is going to get the list of students
- /Students/1 if the verb is GET going to fetch student where the student id is 1
- /Students if the verb is POST, it going to create a new student
- /Students/1 if the verb is PUT it is going to update the student whose id is 1
- /Students/1 if the verb is DELETE, then it is going to delete a record where the id is 1.
Another concept that is related to Uniform Interface is HATEOAS. The HATEOAS stands for Hypermedia as the Engine of Application State. This states that in each request there may be a set of hyperlinks that will let you know what other actions or operations you can perform on the resource. If this is not clear at the moment, don’t worry, we will discuss the concept in a later article.
There are 2 other constraints as well
The REST allows us to use a layered system architecture where we deploy the APIs in server A, and store data on server B and authenticate requests in server C., For example, a client cannot simply tell whether he is connected directly to the server or to an intermediary server on the way.
The code on Demand (optional):
The code on Demand constraint is an optional constraint. We will discuss this constraint in a later article.
What is the difference between REST and SOAP?
Let us discuss the difference between the REST and SOAP service:
- The term SOAP stands for Simple Object Access Protocol whereas the term REST stands for Representational State Transfer.
- SOAP is based on XML protocol which is built on top of the HTTP or sometimes TCP/IP or SMTP whereas the REST is actually not a protocol rather it is an architectural pattern i.e. resource-based architecture.
- The SOAP provides specifications for the implementation of both stateless and state-full whereas REST is completely based on stateless implementation.
- SOAP enforces the message format to be XML whereas REST does not enforce the message format as XML or JSON.
- The SOAP message contains an envelope which includes the SOAP headers and SOAP body where we actually store the information or data that we want to send whereas the REST uses the built-in HTTP headers (with a variety of media-types) to store the data that we want to send and then uses the HTTP Verb such as GET, POST, PUT and DELETE methods to perform the CRUD operations.
- SOAP uses the interfaces and named operations to expose the service for the client to consume whereas the REST uses URI to expose the resources and methods such as GET, POST, PUT, and DELETE.
- The SOAP Performance is slow when it compared to REST.
What are the Differences between the WCF Service and Web API Service? When to choose one over the other?
The term WCF stands for Windows Communication Foundation and it is one of the most popular choices available in .NET Framework for creating the REST services. The problem with WCF is that we need to do a lot of configuration to turn a WCF service into a REST service. So, if you want to develop a REST service nowadays, then the first and most preferable choice is ASP.NET Web API, which is specifically designed for this purpose i.e. to build REST Services.
Then the question that comes to our mind when should we use WCF? let discuss that.
The WCF is basically used to develop the services which are transport/protocol independent. For example, you want to build a single service which can be consumed by 2 different clients – Let’s say, a Java client and .NET client. The Java client wants the protocol (transport protocol) to be HTTP and he also wants the message to be in XML format for interoperability, whereas the .NET client wants the protocol (transport protocol) to be TCP and he wants the message to be in the binary format for performance. If this is the scenario, then WCF is the right choice. What you need to do here is, just develop a single WCF service, and then expose 2 endpoints one for each client (i.e. one endpoint for the Java client and the other endpoint for the .NET client).
The thing that you need to remember is that you can use the WCF to create the REST services. The problem is it’s bit more configuration to turn a WCF Service to WCF Rest Service. So, if you are stuck with the .NET Framework 3.5 or you have an existing SOAP service and you want to add REST to reach more clients, then use WCF.
If you don’t have the limitation of .NET Framework 3.5 or if you want to develop a brand new restful service then go for ASP.NET Web API.
Why Do I need to choose ASP.NET WEB API?
Nowadays, a web application is not sufficient or enough to reach all its customers. Peoples are becoming very smart; they are using different types of devices such as mobile, iPhone, tablets, etc. in their daily life. These devices are having a lot of apps which makes their life easy. In simple words, we can say that we are moving towards the apps world from the web.
So, if we want to expose our data (business data) to the browsers as well as to all these modern devices apps in a fast, secure and simple way, then we should have an API that should be compatible with browsers as well as all these modern devices.
The ASP.NET WEB API is a great framework for building HTTP services that can be consumed by a broad range of clients including browsers, mobiles, iPhones, and tablets.
What is the difference between WCF and WEB API and WCF REST and Web Service?
The .NET framework has a number of technologies that allow us to create HTTP services such as Web Service, WCF, WCF Rest and now WEB API. Following are the differences between these four:
- The Web Service is based on SOAP and it returns the data in XML format.
- It supports only the HTTP protocol.
- Web Service is not an open source but it can be consumed by any client who understands XML.
- It can only be hosted on IIS.
- WCF is also based on SOAP and it also returns the data in the form of XML.
- Unlike Web service, WCF supports different types of protocols (transport protocol) such as TCP, Named Pipes, HTTP, HTTPS, and MSMQ.
- The main problem with WCF service it required lots of configuration which is a headache for a developer.
- Like Web Service, WCF is also not open source but it can be consumed by any client who understands XML.
- WCF can be host within in the application or on IIS or using window service.
- To use a WCF service as WCF Rest service we have to enable webHttpBindings.
- WCF Rest supports the HTTP verbs such as GET and POST by using the [WebGet] and [WebInvoke] attributes respectively.
- To use other HTTP verbs you have to do some configuration in the IIS so that it will accept the request of that particular verb on .svc file
- It supports different data formats such as XML, JSON, and Atom format.
- The Web API Framework is a new framework that is basically used for developing HTTP based services in easy and simple way.
- Unlike WCF Rest Service, it uses the full HTTP features such as URIs, request/response headers, caching, versioning, and various data formats.
- The ASP.NET Web API also supports most of the MVC features such as routing, controllers, actions, filter, model binders, IOC container, dependency injection, unit testing which makes it more simple and robust.
- WEB API Services can be hosted on IIS or within the application
- The Responses in Web API Services are formatted by MediaTypeFormatter into JSON, XML or any custom format you want.
What is the difference between ASP.NET MVC and ASP.NET Web API?
The differences between the ASP.NET MVC and ASP.NET Web API are as follows
- ASP.NET MVC is used to create web applications that return both views and data but ASP.NET WEB API is used to create rest full HTTP services with an easy and simple way that returns only data, not view.
- WEB API helps to build REST-full services over the .NET Framework and it also supports content-negotiation(it’s about deciding the best response format data that could be acceptable by the client. it data format may be JSON, XML, ATOM or any other custom formatted data) which are not in MVC.
- WEB API also takes care of returning data in a particular format like JSON, XML or any other based upon the Accept header in the request. The ASP.NET MVC Framework can return data only in JSON format using the JsonResult return type.
- In WEB API the request is mapped to the actions based on HTTP verbs but in MVC it is mapped to the action name.
- We can mix WEB API and MVC controller in a single project to handle advanced AJAX requests which may return data in JSON, XML or any others format and build a full-blown HTTP service. Typically, this will be called WEB API self-hosting.
- Moreover, WEB API is lightweight architecture and except for the web application, it can also be used with smartphone apps.
In the next article, I am going to discuss the step by step procedure to create ASP.NET Web API application from scratch. Here, in this article, I gave a brief introduction to ASP.NET Web API. I hope this article will help you with your needs.