April flash sale

ASP.NET Web API Interview Questions and Answers

Web API is an application programming interface for the web. There are four various types of APIs used in web services, namely, public, private, partner, and composite. Web APIs let developers create complex functionality easily without any hurdles. No matter if you are a beginner or an intermediate or an expert of Web API, this guide will aid you in increasing your confidence and knowledge of API. The Web API interview questions & answers will not only help you deal with the toughest of the 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.

  • 4.6 Rating
  • 20 Question(s)
  • 15 Mins of Read
  • 2579 Reader(s)

Beginner

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

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: 

  • ASP.NET framework ships out with the .NET framework and is Open Source  
  • Create resource-oriented services using the full features of HTTP. 
  • Web API is a lightweight architecture and is good for the devices which have limited bandwidth 
  • We can expose services to a variety of clients easily like browsers or mobile devices, etc. 
  • Web API increases TDD (Test Data Driven) approach in the development of RESTful services. 

Web API

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.

This is frequently asked in Web API interview questions.  

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 

  • URN: Uniform Resource Name 
  • URL: Uniform Resource Locator 

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: 

http://www.testname.net/asp-net-webapi

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.

Expect to come across this popular question in Web API c#  interview questions.   

Intermediate

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. 

  • GET – The GET method is used to get or retrieve the information from the respective server using a given URI. 
  • HEAD – This is the same as GET, but transfers the status line and header section only. 
  • PUT – It is used for update and it replaces all current resources with the uploaded content. 
  • POST – A POST request is used to send data to the respective server. 
  • DELETE – Deletes or removes all current resources given by a URI. 
  • OPTIONS – Describes the communication options for the target resource. 
  • CONNECT – Establishes a tunnel to the server identified by a given URI. 
  • TRACE – Performs a message loop-back test along the path to the target resource. 

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:

  • 1xx: Informational-It means the request has been received and the process is continuing. 
  • 2xx: Success-It means the action was successfully received, understood, and accepted. 
  • 3xx: Redirection-It means further action must be taken in order to complete the request. 
  • 4xx: Client Error-It means the request contains incorrect syntax or cannot be fulfilled 
  • 5xx: Server Error-It means the server failed to fulfill an apparently valid request. 

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.

A must-know for anyone heading into Web API interview, this is frequently asked in Web API interview questions.  

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's no surprise that this one pops up often in Web API scenario based interview questions.  

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 

  • Microsoft has introduced “WebHttpBinding” that needs to be enabled for creating WCF RESTful Services. 
  • HTTP Methods are mapped to attributes. So, for each method there must be attributes like – “WebGet” for GET and “WebInvoke” for POST verbs. 

Web API 

  • Unlike WCF Rest we can use the full features of HTTP in Web API. 
  • Web API can be hosted in IIS or in applications. 

WCF REST
ASP.NET Web API
1“WebHttpBinding” to be used for creating WCF RestFul services.
WebAPI Supports full features of HTTP.
2HTTP 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.

JSON stands for JavaScript Object Notation and is also used to store and transport data. JSON is often used when data is sent from a server to a web page. JSON is "self-describing" and easy to understand. JSON is a lightweight format for storing and transporting data.

A common in Web API interview questions, don't miss this one.  

Using ASP.NET Web API comes with a number of advantages, the core advantages being: 

  • It works the HTTP way using standard HTTP verbs like GET, POST, PUT, DELETE, etc. for all CRUD operations 
  • Routing: Full support for routes/routing 
  • Response generated in JSON or XML format using MediaTypeFormatter 
  • Self Hosting: It has the ability to be hosted in IIS as well as self-hosted outside of IIS 
  • Model Bindings: Supports Model binding and Validation 
  • Odata: Support for Odata 
  • Filters 
  • Content Negotiation 
  • Link generation to related resources that incorporates routing rules 
  • Ability to create custom help and test pages using ApiExplorer 

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. 

  • Both Web API and WCF can be self-hosted or can be hosted on the IIS Server.  


WCF
WebAPI
1Protocol
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

One of the most frequently posed Web API interview questions, be ready for it.  

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: 

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

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. 

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.

Advanced

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: 

  • Everything is a resource i.e. File, Video, Images, WebPage etc. 
  • Every Resource is identified by a Unique Identifier. 
  • Be Stateless- Every request should be an independent request. 
  • Use simple and Uniform Interfaces. 
  • Everything is done via representation (sending requests from client to server and receiving responses from server to client). 

RESTFUL: It is a 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. 

A staple in Web API interview questions for experienced, be prepared to answer this one.  

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.

This is a regular feature in Web API interview questions, be ready to tackle it.  

WCF 

  • It is a framework built for building or developing service-oriented applications. 
  • It is designed to exchange standard SOAP-based messages 
  • WCF can be consumed by clients which can understand XML. 
  • WCF supports a variety of transport protocols like – HTTP, TCP, Named Pipes or MSMQ etc. 
  • iWCF ships out with the .NET Framework.  
  • WCF service is good for Message Queue, duplex communication, one-way messaging. 

Web API 

  • Web API is an open source platform. 
  • It is a framework which helps us to build/develop HTTP services using HTTP verbs 
  • It supports most of the MVC features which keep Web API over WCF. 
  • The purpose of Web API framework is to generate HTTP services that reach more clients by generating data in raw format, for example, plain XML or JSON string. So, ASP.NET Web API creates simple HTTP services that render raw data. 
  • This returns XML or JSON to client. 
  • All requests are mapped to actions using HTTP verbs. 
  • We cannot return a view from WebAPI 
  • In the ASP.NET Web API request is mapped to Action based on the Action Verbs. 

MVC 

  • ASP.NET MVC is used to create a web application which returns both data as well as View (HTML) whereas Web API is used to create HTTP based Services which only returns data and not view.  
  • ASP.NET MVC facilitates in easyrendering of HTML. 
  • MVC is used to create a web app, in which we can build web pages. 
  • JSONit will return JSONResult from action method. 
  • All requests are mapped to the respective action methods. 
  • We can return a view from MVC 
  • In an ASP.NET MVC application, requests are mapped to Action Methods. 

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: 

  • Client and Server Separation: Server and Clients are clearly isolated in the RESTful services. 

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: REST Architecture is based on the HTTP Protocol and the server response can be cached by the clients, but no client context would be stored on the server. 

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.  

  • Uniform Interface: Allows a limited set of operations defined using the HTTP Verbs. For eg: GET, PUT, POST, Delete etc. 

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).  

  • Cacheable: RESTful architecture allows the response to be cached or not. Caching improves performance and scalability. 

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.  

  • Code-On-Demand:is any technology that sends executable software code from a server to a client upon request from the client. The program code lies inactive on a web server until a client requests a web page that contains a link to the code using the client's web browser. Upon this request, the web page and the program are transported to the user's machine using HTTP. When the page is displayed, the code is started in the browser and executes locally, inside the user's computer until it is stopped (e.g., by the user leaving the web page). 
  • 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.  

This is frequently asked in Web API interview questions for experienced.  

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 is 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.

Expect to come across this advanced Web API interview question.  

Description

Web API is an application programming interface for the web. There are four various types of APIs used in web services, namely, public, private, partner, and composite. Web APIs let developers create complex functionality easily without any hurdles. No matter if you are a beginner or an intermediate or an expert of Web API, this guide will aid you in increasing your confidence and knowledge of API. The Web API interview questions & answers will not only help you deal with the toughest of the 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.
Levels