Most Asked Web API Interview Questions
Following is a list of frequently asked Web API interview questions and their best possible answers.
1) What is a Web API? / What do you understand by Web API?
In Web API, API stands for Application Programming Interface. It is a framework that helps users to develop or build HTTP services that can be used by various clients, including browsers and mobile devices. For example, The ASP.NET Web API helps us to build these services on the .NET framework.
2) Why use Web API? / Why is Web API preferred to use? / What is the reason behind using Web API?
We have many other technologies similar to Web API, but there are several genuine reasons to use Web API.
3) Is it possible to use RESTful services using WCF? / Why is the use of Web API preferred over RESTful services?
Yes, it is possible to use RESTful services using WCF. We can easily develop RESTful services with WCF, but two main reasons make users choose Web API instead of RESTful services.
4) Is it true that ASP.NET Web API has replaced WCF?
No, it is not true at all that ASP.NET Web API has replaced WCF. ASP.NET Web API is another way to build non-SOAP-based services, i.e., plain XML or JSON string.
Web API is lightweight due to using HTTP only so. It cannot match the power and flexibility of WCF. Web API is perfect if you only need to use HTTP as your transport. But if you want to use a different protocol such as TCP or Named Pipes, you would have to use WCF.
5) What is the main difference between Web API and WCF?
WCF stands for Windows Communication Foundation. It is used for service-oriented application development supporting various transport protocols such as HTTP, TCP, MSMQ. The WCF clients must be able to understand XML. On the other hand, Web API is designed specifically for HTTP services, and due to its low-bandwidth, it supports non-SOAP services and most MVC features.
6) What are the main advantages of using Web API?
Following are the main advantages of using Web API:
7) What are the differences between Web API and MVC?
Following are the main differences between Web API and MVC (Model View Controller)
8) Which .NET framework supports Web API?
The .NET framework version 4.0 and above versions support web API.
9) What are the main return types supported in Web API?
Following are the main return types of Web API controller action:
10) Which open-source library is supported by Web API for JSON serialization?
Web API uses JSON.NET library for JSON serialization.
11) What is the biggest drawback of "Other Return Types" in Web API?
The biggest disadvantage of the "Other Return Types" in Web API is that it doesn't help us directly return an error code like a 404 error.
12) What do you understand by Web API Routing?
Web API Routing is a pattern matching just similar we do in MVC. All routes are registered in Route Tables.
13) Is it possible to use ASP.NET Web API in applications created using other than .NET language?
Yes, we can easily use ASP.NET Web API in the applications created using another language than .NET, but those applications must have access/support to the HTTP protocol.
14) What do you understand by SOAP?
The full form of SOAP is Simple Object Access Protocol. It is an XML message format used in web service interactions to interchange data. It facilitates users to send messages over HTTP or JMS. It is also an XML-based messaging protocol for exchanging information among computers.
In other words, we can say that SOAP is a messaging protocol used for interchanging data in a decentralized and distributed environment.
15) How to use the Web API with ASP.NET Web Form?
We can use the Web API with ASP.NET Web Form by performing the following three simple steps:
16) How can we restrict or limit access to Web API to Specific HTTP Verb?
We can easily restrict or limit access to an ASP.NET Web API method called using a particular HTTP method. In this process, attribute programming plays a crucial role.
17) What do you understand by the RESTful Services?
The full form of REST stands for the Representational State Transfer. Roy Fielding coined this term in 2000. RESTful is an architectural style for creating loosely coupled applications over HTTP. To make API to be RESTful, we have to follow the six constraints given below:
18) What do you understand by exception filters?
Exception filters are used to execute when exceptions are unhandled and thrown from a controller method. There may be several reasons for the exception. Exception filters implement the "IExceptionFilter" interface.
19) What do you understand by TestApi in Web API?
TestApi is a utility library of Web APIs. Using this library tester, developers can create testing tools and automated tests for a .NET application using data-structure and algorithms.
20) Is it possible to return View from Web API?
No, Web API does not return View, but they return the data. In Web API, APIController is used for returning the data. If you want to return a view from the controller class, you have to make sure you have used or inherited the Controller class.
21) What is Parameter Binding in ASP.NET Web API?
Parameter Binding is a process that specifies that when a Web API calls a method on a controller, it must set the values for the parameters.
By Default, Web API uses the following rules to bind the parameter:
22) How can you register exception filter globally?
We can easily register exception filter globally by using the following code:
23) What is the difference between REST and RESTful?
Difference between REST and RESTful:
24) What is Content Negotiation in Web API?
Content Negotiation is the process of selecting the best representation for a given response from a set of various multiple representations available. Following are the two main headers that are responsible for the Content Negotiation:
25) Give an example to specify Web API Routing?
Following is an example of Web API Routing:
26) What do you understand by a Media-Type Formatter in ASP.NET Web API?
The Media-Type formatter is an abstract class acting as a parent class of JsonMediaTypeFormatter and XmlMediaTypeFormatter. These two classes, JsonMediaTypeFormatter (which is used to handle JSON format) and XmlMediaTypeFormatter (which is used to handle XML format), are derived from the Media-Type formatter.
The Media-Type formatter classes are also responsible for serializing the response data in the client's format.
27) What is the method to handle errors in Web API?
Several classes are available in Web API, such as HttpError, Exception Filters, HttpResponseException, and Registering Exception Filters, etc., to handle errors.
28) What are the several new features that come with ASP.NET Web API 2.0?
Following are the latest new features of ASP.NET Web API framework v2.0:
29) How can you restrict access methods to specific HTTP verbs in Web API?
It is very easy to implement access restrictions in Web API with Attributes' help (like HTTP verbs). We can do it by defining HTTP verbs as an attribute to restrict access.
30) What is Authorize Attribute? What is its use?
Web API provides a built-in authorization filter known as Authorize Attribute. This filter is used to check whether the user is authenticated or not. If not, the user will see 401 Unauthorized HTTP Status Code.
31) Is it possible to pass multiple complex types in Web API?
Yes, we can easily pass multiple complex types in Web API by using the following two methods:
32) What do you understand by basic HTTP Authentication?
Basic HTTP Authentication is a mechanism that is used for user authentication. Here, users are authenticated through the service in which the client passes username and password in the HTTP Authorization request headers. The credentials are formatted as the string "username:password," based on encoded.
33) How can you pass an ArrayList in Web API?
The following example shows the code how we can pass an ArrayList in Web API:
34) How can the Web API route the HTTP request to the Controller ASP.NET MVC?
In ASP.NET Web API, an HTTP request is used to map to the controller. The Web API framework uses a routing table to determine which action is to invoke.
35) How can we handle an error using HttpError in Web API?
In Web API, the HttpError method is used to throw the response body's error info. Along with this, the "CreateErrorResponse" method can also be used, which is an extension method defined in "HttpRequestMessageExtension."
36) How can you register exception filter globally?
We can use the following code to register an exception filter globally:
37) What do you understand by Exception handling in Web API?
Exception handling is a technique used to handle runtime error in the application code. There are several ways that we can use to handle the error in ASP.NET Web API. Following is a list of some of them:
38) What is Web API Versioning, and why is it used?
Web API Versioning is a technique in which Web API is arranged to cope with the business changes, and the API will not impact the client that is using/consuming the existing API. As we know, multiple clients can consume the Web API at a time, so Web API versioning is necessary and required as the business grows, and business requirement changes with the time.
39) How many ways are you to do Web API Versioning?
Following are the several ways to do Web API Versioning:
40) What are the advantages of Web API over WCF?
WCF services use the SOAP protocol while Web API never uses the SOAP protocol. That's why Web API services are lightweight since it doesn't use SOAP. It also reduces the data which is transferred to resume service. The biggest advantage of Web API is that it never needs too much configuration. Therefore, the client can interact with the service by using the HTTP verbs.
41) What is the difference between MVC and Web API?
Differences between MVC and Web API:
42) Who can use or consume Web API?
Any client which supports HTTP verbs such as GET, PUT, DELETE, POST etc., can use or consume Web API. Web API services don't need any configuration, so it is very easy to consume them by any client. Even portable devices like mobile devices can easily consume them, and it is surely the biggest advantages of this technology.
43) What are the different HTTP methods used in Web API?
Following are the different types of HTTP methods used in Web API. We can use them according to the requirements.
Note: From the list of all the above methods, the GET, PUT, POST, and DELETE are the most popular methods. All methods are case-sensitive, and we must use them in uppercase.
44) What do you understand by the HTTP Status Codes?
The Response Header of each API response consists of an HTTP Status Code. The HTTP Status Codes are the three-digit integers that contain the server response. Here, each number specifies a meaning. The first digit of the Status-Code defines the class of response. According to this first number, the HTTP Status Codes are categorized into five types.
Following is the list of HTTP Status Codes with their meaning:
45) What are some commonly seen HTTP Status Codes?
Following is the list of some commonly seen HTTP Status Codes:
46) What do you understand by Internet Media Types?
The Internet Media Type is a file identification mechanism on the MIME encoding system. It is also known as the "MIME type." It has become the de facto standard for identifying content on the Internet.
For example, suppose we receive an email from a server with an attachment, then the server embeds the media type of the attachment in the message header. So, the browser can launch the appropriate helper application or plug-in.
47) What is the difference between XML and JSON?
Following are the differences between XML and JSON:
48) In which .NET framework the ASP .NET Web API was introduced?
The first version of ASP.NET Web API was introduced in .NET Framework 4. After that, all the later versions of the .NET Framework support the ASP.NET Web API.
49) What are the main differences between WCF vs. MVC vs. Web API?
WCF vs. MVC vs. ASP.NET Web API:
50) How can we unit test the Web API?
We can use the Fiddler tool to unit test the Web API. Follow the steps given below and do the setting in the Fiddler tool:
Compose Tab -> Enter Request Headers -> Enter the Request Body and execute.
51) How can you return JSON from Web API Service irrespective of the Accept header value?
We can easily return JSON from Web API Service irrespective of the Accept header value by removing the XmlFormatter from the Register() method of WebApiConfig.cs file, which is present inside the App_Start folder. Use the following piece of code to remove XmlFormatter completely. It will always return data in JSON format irrespective of the Accept header value in the client request.
52) What is the difference between Controller and ApiController?
Controller is used to rendering our normal views.
ApiController is used when we have only to return data that is serialized and sent to the client.