Did you ever feel that you need that extra push before you appear for a job interview? This is where we come to your assistance. Our Web API Interview Questions & Answers will not only help you deal with the toughest of the Web API interview questions, but also guide you in dealing with every technical question related individually, be it HTTP, ASP.NET Web API, Resources and URIs, WCF vs ASP.NET Web API etc. These 20 Web API Interview Questions & Answers are created by the best in the business and help you in achieving your dreams of getting the Web API Developer job you have always aspired for.
ASP.NET Web API is a framework provided by the Microsoft open source technology for building and consuming HTTP based services on top of .NET Framework. The advantage of Web API is that it can be consumed by a broader range of clients including web browser and mobile applications, IoT etc. ASP.NET Web API provides an ideal platform for building RESTful applications on the .NET Framework. So there will be a client server communication using HTTP protocol.
HTTP (Hypertext Transfer Protocol) is an application level protocol that is used to deliver data such as html files, image files, query results, etc. on the World Wide Web. HTTP is a stateless protocol, meaning that after one cycle of request and response the server forgets everything about the cycle, and it considers another request from the same client as a new request from a new client.
Below is a simple Client-Server architecture of HTTP protocol
Using ASP.NET Web API, we can create non-SOAP based services like plain XML or JSON strings, etc. with many other advantages including:
The clients which support HTTP verbs such as GET, PUT, DELETE, POST consume WebAPI services. As WebAPI services are very easy to consume by any client because they don’t need any configuration. Portable devices like Mobile devices, IoT can easily consume WebAPI which is certainly the biggest advantages of this technology.
URI is a string of characters used to identify a resource on the internet either by location or by name, or by both. URI stands for Uniform Resource Identifier.
URI is used to identify resources, for example, in the real world assume there is a person named “Test Person” who lives in “403, Test City, World”. We can find that person by address or by name or by both.
URI can be categorized into 2 as URL and URN
URI is used to send requests to the server. This can be achieved with the help of both URN and URL. Using URN is inefficient, as there can be many resources with the same name. So the most widely used method is URL. URL consists of two required components The Protocol & The Domain.
Look at the following URL:
Here the red colored part is the protocol and the black colored part is the domain, other parts are optional. It may contain the port/path/query strings/fragments.
There are various types of HTTP methods and they can be used according to the requirements. These methods are case-sensitive and they must used in uppercase.
Among these methods GET/PUT/POST/DELETE are the most popular methods.
HTTP Status Codes are the 3 digit integers which contain in server response. There is a meaning for each number. Response Header of each API response contains the HTTP Status Code. The first digit of the Status-Code defines the class of response. HTTP Status Codes are grouped into five categories based upon the first number.
Following are the set of status codes with its meaning:
Some of the commonly seen HTTP Status Codes are: 200 (Request is Ok), 201 (Created), 202 (Accepted), 204 (No Content), 301 (Moved Permanently), 400 (Bad Request), 401 (Unauthorized), 403 (Forbidden), 404 (Not Found), 500 (Internal Server Error), 502 (Bad Gateway), 503 (Service Unavailable) etc.
HTTP header fields are components of the header section of request or response messages in the Hypertext Transfer Protocol. It defines the operating parameters of an HTTP transaction. There are more contents in an HTTP header field.
For example, HTTP request to fetch aspnetwebapi.htm page from the web server running on developmenthelp.net.
Media Type is a file identification mechanism on the MIME encoding system, formerly “MIME type”. The Internet media type has become the de facto standard for identifying content on the Internet.
For example, consider an instance of receiving an email from a server with an attachment, 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.
It is difficult to say which one is better between Web API and WCF Rest service. Both the technologies have their own significant in the context of development, hosting and consuming the service.Although both WCF REST and ASP.NET Web API follow the REST architecture, these have the following differences:
|WCF REST||ASP.NET Web API|
|1||“WebHttpBinding” to be used for creating WCF RestFul services.||WebAPI Supports full features of HTTP.|
|2||HTTP Methods are mapped to attributes for example GET for WebGet and POST for WebInvoke.||It possible to host Web API in IIS as well as in an application.|
XML stands for eXtensible Markup Language. It is a markup language like HTML and is designed to store and transport data. XML doesn’t do anything, but it can store data in a specific format.
Using ASP.NET Web API comes with a number of advantages, the core advantages being:
Windows Communication Foundation is designed to exchange standard SOAP-based messages using a variety of transport protocols like HTTP, TCP, NamedPipes or MSMQ, etc. WCF i.e. Windows Communication Foundation is a framework used for building Service Oriented applications (SOA) and supports multiple transport protocols like HTTP, TCP, MSMQ, etc. WCF ships out with the .NET Framework. WCF service is good for Message Queue, duplex communication, one-way messaging. WCF supports SOAP and XML formats.
ASP.NET API is a framework for building non-SOAP based services over HTTP only. Web API is a Framework to build HTTP Services that can reach a board of clients, including browsers, mobile, IoT Devices, etc. and provided an ideal platform for building RESTful applications. It is limited to HTTP based services. ASP.NET framework ships out with the .NET framework and is Open Source. One major advantage of Web API is that it is the best fit with MVC pattern. Web API supports any media format including JSON, XML.
|1||Protocol||Multiple protocol support (like HTTP, TCP, Named Pipes and more)||Supports only HTTP protocol|
|Format||SOAP is the default format. SOAP message has specific format. Supports SOAP and XML Format||Web API supports any media format including XML and JSON.|
|Size||Heavy weight because SOAP messages have more than just data||Light Weight because only required information is passed.|
|Principles||Follows WS-* specification||Follows REST Principles|
|Configuration||Need lot of configuration to run||Web API is simple and no configuration needed to run.|
|Service||WCF is good for developing service-oriented applications,||While ASP.Net Web API is perfect for building HTTP services. Web API is best fit with MVC pattern.|
|For each method there must be attributes like – “WebGet” and “WebInvoke”||Web API supports HTTP protocol verbs for communication.|
|For REST service we need to use attribute “WebInvoke”.||Unlike WCF Rest we can use the full features of HTTP in Web API|
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 full features of HTTP and reaching more clients such as mobile devices etc.
But WCF is still a good choice for the following scenarios:
The difference between REST and SOAP is given below:
First Version of ASP.NET Web API is introduced in .NET Framework 4. After that, all the later versions of the .NET Framework support the ASP.NET Web API.
REST or Representational State Transfer is an architectural style for designing applications. Instead of using complex mechanisms like CORBA, RPC or SOAP for communication, it dictates that HTTP should be used.
There are a few principles associated with REST architectural style:
RESTFUL: It is term written by applying REST architectural concepts and is called RESTful services. It focuses on system resources and how the state of the resource should be transported over HTTP protocol.
It completely depends upon the requirement. Choose ASP.NET Web API if you want HTTP based services only, as Web API is a lightweight architecture and is good for the devices which have limited bandwidth. We can also create the REST services with WCF, but that requires a lot of configuration. In case you want a service that should support multiple transport protocols like HTTP, UDP, TCP, etc. then WCF will be the better option.
REST stands for Representational State Transfer. This term was coined by Roy Fielding in 2000. RESTful is an Architectural style for creating loosely coupled applications over HTTP. In order to make API to be RESTful, it must adhere to the around 6 constraints that are mentioned below:
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.
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.
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 verbs (GET, PUT, POST, and DELETE) that are sent with each request tell the API what to do with the resource. Each resource is identified by a specific URI (Uniform Resource Identifier).
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 lets the client know how long this data holds good, so that the client does not have to come back to the server for that data over and over again.
We can unit test the Web API using the Fiddler tool. Below are the settings to be done in Fiddler –Compose Tab -> Enter Request Headers -> Enter the Request Body and execute.
Most of the people give the wrong answer for this question. The right answer is No, we can’t. Web API does not return View but returns the data. As we discussed in an earlier interview question about the difference between ASP.NET MVC and Web API, ASP.NET Web API creates HTTP services that render raw data. Although, it’s quite possible in ASP.NET MVC application, the APIController is meant for returning the data. So, if you need to return a view from the controller class, then make sure to use or inherit the Controller class.