Web API Interview Questions and Answers

Web API Interview Questions and Answers (Basics)

In this article, I am going to discuss the most frequently asked Web API Interview Questions and Answers. In the next article, I am going to discuss the Experienced Web API Interview questions and answers.

Let start the Web API Interview Questions and Answers discussion with the most basic question that asked in almost in all interviews i.e. What is ASP.NET Web API.

What is ASP.NET Web API?

The term API stands for Application Programming Interface. ASP.NET Web API is a framework that makes it easy to build Web API’s, i.e. HTTP based services on top of the .NET Framework. ASP.NET Web API is an ideal platform for building Restful services. These services can then be consumed by a broad range of clients like

  1. Browsers
  2. Mobile applications
  3. Desktop applications
  4. IOTs
What is Rest?

REST stands for Representational State Transfer. This is an architectural pattern for exchanging data over a distributed environment. REST architectural pattern treats each service as a resource and a client can access these resources by using HTTP protocol methods like GET, POST, PUT, and DELETE.

What is Not Rest?
  1. A protocol
  2. A standard
  3. A replacement of SOAP

What are RESTful services?

If you are preparing for Web API Interviews then definitely you have to prepare this Web API Interview Questions.

REST stands for Representational State Transfer. The REST was first introduced in the year 2000 by Roy Fielding as part of his doctoral dissertation. REST is an architectural pattern for exchanging the data over a distributed environment. REST architectural pattern treats each service as a resource and a client can access these resources by using HTTP protocol methods like GET, POST, PUT, and DELETE. The REST architectural pattern specifies a set of constraints that a system should adhere to. Here are the REST constraints.

Client-Server constraint –

This is the first constraint. 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 and server-side logic. That means client application and server application should be developed separately without any dependency on each other. A client should only know resource URIs and that’s all. Severs and clients may also be replaced and developed independently as long as the interface between them is not altered.

Stateless constraint –

The next constraint is the stateless constraint. The stateless constraint specifies that the communication between the client and the server must be stateless between requests. This means that we should not be storing anything on the server related to the client. The request from the client should contain all the necessary information for the server to process that request. This ensures that each request can be treated independently by the server.

Cacheable constraint –

Some data provided by the server like the list of products, or list of departments in a company does not change that often. This constraint says that let the client know how long this data is good for so that the client does not have to come back to the server for that data over and over again.

Uniform Interface constraint –

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. In the context of a REST API, resources typically represent data entities. The product, Employee, Customer, etc. are all resources. The HTTP verb (GET, PUT, POST, and DELETE) that is sent with each request tells the API what to do with the resource. Each resource is identified by a specific URI (Uniform Resource Identifier).

Layered System- 

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 ordinarily tell whether it is connected directly to the server or to an intermediary along the way.

What are the differences between REST and SOAP?

It is one of the most frequently asked Web API Interview Questions in Web API Interviews.

The difference between REST and SOAP is given below:

  1. SOAP stands for Simple Object Access Protocol whereas REST stands for Representational State Transfer.
  2. The SOAP is an XML based protocol whereas REST is not a protocol but it is an architectural pattern i.e. resource-based architecture.                         
  3. SOAP has specifications for both stateless and state-full implementation whereas REST is completely stateless.
  4. SOAP enforces message format as XML whereas REST does not enforce message format as XML or JSON.
  5. The SOAP message consists of an envelope which includes SOAP headers and body to store the actual information we want to send whereas REST uses the HTTP build-in headers (with a variety of media-types) to store the information and uses the HTTP GET, POST, PUT and DELETE  methods to perform CRUD operations.
  6. SOAP uses interfaces and named operations to expose the service whereas to expose resources (service) REST uses URI and methods like (GET, PUT, POST, DELETE).
  7. SOAP Performance is slow as compared to REST.
What are the Differences between WCF and Web API? When to choose one over the other?

WCF (Windows Communication Foundation) is one of the choices available in .NET for creating both SOAP and REST services. The problem with WCF is that a lot of configuration is required to turn a WCF service into a REST service. So the more natural choice for creating REST services is ASP.NET Web API, which is specifically designed for this purpose.

WCF is more suited for building services that are transport/protocol independent. For example, we want to build a single service that can be consumed by 2 different clients – Let’s say, a Java client and .NET client. Java client wants the transport protocol to be HTTP and message format to be XML for interoperability, whereas the .NET client expects the protocol to be TCP and the message format to be binary for performance. For this scenario, WCF is the right choice. What we need to do here is create a single WCF service, and then configure 2 endpoints one for each client (i.e. one for the Java client and the other for the .NET client).

There is nothing wrong to use WCF to create REST services. It’s just that it’s a bit more complex and configuration can be a headache. If we are stuck with .NET 3.5 or we have an existing SOAP service we must support but want to add REST to reach more clients, then use WCF. 

If we don’t have the limitation of .NET 3.5 and we want to create a brand new restful service then use ASP.NET Web API.

WCF
  1. It is a framework used for building or developing service-oriented applications. 
  2. WCF can only be consumed by clients, which can understand XML. WCF supports protocols like – HTTP, TCP, Named Pipes etc.
Web API
  1. It is a framework which helps us to build/develop HTTP services
  2. Web API is an open source platform.
  3. It supports most of the MVC features which keep Web API over WCF.
When do we need to choose Web API?

Today, a web-based application is not enough to reach its customers. Now a day, Peoples become very smart; they are using iPhone, mobile, tablets etc. devices in their daily life. These devices are having a lot of apps for making their life easy. Actually, we are moving from the web towards apps world.

So, if we like to expose our service data to the browsers as well as to all these modern devices apps in a fast and simple way, we should have an API which is compatible with browsers as well as all these 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, iPhone and tablets.  Moreover, WEB API is open source and an ideal platform for building REST-full services over the .NET Framework.

What are the advantages of using REST in Web API?

REST always used to make fewer data transfers between client and server which makes REST an ideal for using it in mobile apps. Web API supports HTTP protocol thereby it reintroduces the old way of HTTP verbs for communication.

What are the Differences between WCF Rest and Web API?

WCF Rest
  1. WebHttpBinding to be enabled for WCF Rest.
  2. For each method, there have to be attributes like – “WebGet” and “WebInvoke”
  3. For GET and POST verbs respectively.
Web API
  1. Unlike WCF Rest we can use full features of HTTP in Web API.
  2. Web API can be hosted in IIS or in an application.
What are the advantages of using ASP.NET Web API?

Using ASP.NET Web API has a number of advantages, but core advantages are:

  1. It works the way HTTP works using standard HTTP verbs like GET, POST, PUT, DELETE, etc. for all CRUD operations
  2. Complete support for routing
  3. Response generated in JSON or XML format using MediaTypeFormatter
  4. It has the ability to be hosted in IIS as well as self-host outside of IIS
  5. Supports Model binding and Validation
  6. Support for OData
What new features are introduced in ASP.NET Web API 2.0?

The new features introduced in ASP.NET Web API framework v2.0 are as follows:

  1. Attribute Routing
  2. External Authentication
  3. CORS (Cross-Origin Resource Sharing)
  4. OWIN (Open Web Interface for .NET) Self Hosting
  5. IHttpActionResult
  6. Web API OData
What are the differences between ASP.NET MVC and ASP.NET Web API?

Below are some of the differences between MVC and Web API 

MVC
  1. MVC is used to create a web app, in which we can build web pages.
  2. For JSON it will return JSONResult from an action method.
  3. All requests are mapped to the respective action methods.
Web API
  1. This is used to create a service using HTTP verbs
  2. This returns XML or JSON to the client.
  3. All requests are mapped to actions using HTTP verbs.
There are following differences between ASP.NET MVC and WEB API:
  1. 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 the easy and simple way that returns only data, not view.
  2. WEB API helps to build REST-full services over the .NET Framework and it also supports content-negotiation which is not in MVC.
  3. 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. MVC only return data in JSON format using JsonResult.
  4. In WEB API the request is mapped to the actions based on HTTP verbs but in MVC it is mapped to actions name.
  5. 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 building a full-blown HTTP service. Typically, this will be called WEB API self-hosting.
  6. Moreover, WEB API is lightweight architecture and except the web application, it can also be used with smartphone apps.
Is it true that ASP.NET Web API has replaced WCF?

It’s a misconception that ASP.NET Web API has replaced WCF. It’s another way of building non-SOAP based services, for example, plain XML or JSON string, etc.

Yes, it has some added advantages like utilizing the full features of HTTP and reaching more clients such as mobile devices, etc.

But WCF is still a good choice for the following scenarios:

  1. If we intended to use transport other than HTTP, e.g. TCP, UDP or Named Pipes
  2. Message Queuing scenario using MSMQ
  3. One-way communication or Duplex communication
Explain media Formatters in Web API 2

This Web API Interview Questions are asked almost all Web API Interviews.

As we know that web API handles JSON and XML formats based on the Accept and Content-Type header values. But how does web API handles these different formats? The answer is by using Media Type Formatters.

The Media-Type Formatters are classes which are responsible for serializing request/response data so that web API can understand the request data format and send data in the format which client expects.

Technically MediaTypeFormatter is an abstract class from which JsonMediaTypeFormatter and XmlMediaTypeFormatter classes inherit from. JsonMediaTypeFormatter handles JSON and XmlMediaTypeFormatter handles XML.

How to return only JSON from ASP.NET Web API Service irrespective of the Accept header value?

Include the following line in Register() method of WebApiConfig.cs file in App_Start folder. This line of code completely removes XmlFormatter which forces ASP.NET Web API to always return JSON irrespective of the Accept header value in the client request. Use this technique when we want our service to support only JSON and not XML. 

With this change, irrespective of the Accept header value (application/xml or application/json), the Web API service is always going to return JSON.

config.Formatters.Remove(config.Formatters.XmlFormatter);

How to return the only XML from ASP.NET Web API Service irrespective of the Accept header value?

Include the following line in Register() method of WebApiConfig.cs file in App_Start folder. This line of code completely removes JsonFormatter which forces ASP.NET Web API to always return XML irrespective of the Accept header value in the client request. Use this technique when you want your service to support only XML and not JSON.

config.Formatters.Remove(config.Formatters.JsonFormatter);
With this change, irrespective of the Accept header value (application/xml or application/json), the Web API service is always going to return XML.

How to return JSON instead of XML from ASP.NET Web API Service when a request is made from the browser?

So here is what we want the service to do

  1. When a request is issued from the browser, the web API service should return JSON instead of XML. 
  2. When a request is issued from a tool like a fiddler the Accept header value should be respected. This means if the Accept header is set to application/xml the service should return XML and if it is set to application/json the service should return JSON.

There are 2 ways to achieve this

Approach1: 

Include the following line in Register() method of WebApiConfig.cs file in App_Start folder. This tells ASP.NET Web API to use JsonFormatter when a request is made for text/html which is the default for most browsers. The problem with this approach is that the Content-Type header of the response is set to text/html which is misleading.

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue(“text/html”));

Approach2: 

Include the following class in WebApiConfig.cs file in App_Start folder. 

Register Formatter: 

Place the following line in Register() method of WebApiConfig.cs file in App_Start folder

WEB API Interview Questions and Answers

config.Formatters.Add(new CustomJsonFormatter());
With these 2 changes, when a request is issued from the browser you will get JSON formatted data and the Content-Type header of the response is also set to application/json. If you are using tools like a fiddler and if you set Accept header to application/xml you will still get XML formatted data.

Following are the Interview Questions designed for Freshers as well as Experienced.

ASP.NET MVC Interview Questions and Answers

SQL Server Interview Questions and Answers

C#.NET Interview Questions and Answers

In this article, I try to explain most frequently asked Web API Interview Questions and Answers. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *