WEB API Interview Questions
WEB API Interview Questions
Web API (Application Programming Interface), as the name suggests, is an API that can
be accessed over the Web using the HTTP protocol. It is basically considered the best
platform for revealing or uncovering data and services to various different services. It is
a tool that can be used to push data to a server and can be accessed by server code. It
can be built or developed using various technologies like java, ASP.NET, etc.
It does not have any specific data type. It can return data of any type depending upon
the business requirement. There are many HTTP methods like GET, POST, PUT, etc.,
which can return data in different formats depending upon the use case.
4. What is the difference between Web API and WCF?
Web API: It is an application programming interface for both web browsers and web
servers. Browser API simply extends or increases the functionality of web browsers
whereas Server API simply extends or increases the functionality of web server.
Web API is considered the best choice over WCF because of the following reasons:
Web API uses all features of HTTP such as URIs, request/response headers,
caching, versioning, various content formats, etc.
One does not have to define or explain any extra config setting for different
devices in Web API.
Web API uses different text formats including XML because of which it is faster
and more preferred for lightweight services.
Web API also supports MVC features whereas WCF does not support MVC
features.
Web API provides more flexibility as compared to WCF.
Web API uses standard security like token authentication, basic authentication,
etc., to provide secure service whereas WCF uses WS-I standard to provide secure
service.
REST is very important and beneficial in Web API because of the following reasons:
{"city":"Mumbai","state":"Maharashtra"}
SOAP (Simple Object Access Protocol): It is a simple and lightweight protocol that is
generally used for exchanging structured and typed information on the Web. It works
mostly with HTTP and RPC (Remote Procedure Call). This protocol is mainly used for B2B
applications one can define a data contract with it. SOAP messages are heavier in
content and therefore use greater bandwidth.
For example:
<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2001/12/soap-envelope"
SOAP-ENV:encodingStyle=" http://www.w3.org/2001/12/soap-encoding">
<soap:Body>
<Demo.guru99WebService xmlns="http://tempuri.org/">
<EmployeeID>int</EmployeeID>
</Demo.guru99WebService>
</soap:Body>
</SOAP-ENV:Envelope>
REST SOAP
It is basically an architectural pattern. It is basically a messaging protocol.
It usually works with various text
formats such as plain text, HTML,
JSON, XML, etc. It only works with XML formats.
It has some specifications for both stateless and
It is totally stateless. stateful implementation.
Its performance is faster as
compared to SOAP. Its performance is slower as compared to REST.
It uses WSDL (Web Service Description Language)
It uses XML and JSON to send and for communication among consumers or users and
receive data. providers.
SOAP includes built-in error handling for
REST has to resend transfer communications errors using WS-
whenever it determines any errors. ReliableMessaging specification.
It calls services by calling RPC (Remote Procedure
It calls services using the URL path. Call) method.
9. What is Web API 2.0?
It is basically an enhanced and modified feature of Web API. This new version supports
various new features as given below:
Because of all the new features of Web API 2.0, it is considered an optimal choice and
suitable development model that makes it easier to develop RESTful services interfaces
to different clients running on various platforms. It also supports configuring routes in
the Web API method or controller level.
In web API, media type formatters are classes that are responsible for serialization data.
Here, serialization generally means a process of translating data into a format that can
be transmitted and reconstructed later. Because of serializing request/response data,
Web API can understand request data format in a better way and send data in a format
that the client expects. It simply specifies data that is being transferred among client and
server in HTTP response or request.
12. Which of the following Open-source libraries is used by WEB API for
JSON serialization?
Filters are basically used to add extra logic at different levels of Web API framework
request processing. Different types of Web API filters are available as given below:
Authentication Filter: It handles authentication and authenticates HTTP
requests. It also helps to authenticate user detail. It checks the identity of the user.
Authorization Filter: It handles authorization. It runs before controller action.
This filter is used to check whether or not a user is authenticated. If the user is not
authenticated, then it returns an HTTP status code 401 without invoking the action.
AuthorizeAttribute is a built-in authorization filter provided by Web API.
Action Filter: It is attributing that one can apply to controller action or entire
controller. It is used to add extra logic before or after controller action executes. It is
simply a way to add extra functionality to Web API services.
Exception Filter: It is used to handle exceptions that are unhandled in Web API.
It is used whenever controller actions throw an unhandled exception that is not
HttpResponseException. It will implement an “IExceptionFilter” interface.
Override Filter: It is used to exclude specific action methods or controllers from
the global filter or controller level filter. It is simply used to modify the behavior of other
filters for individual action methods.
15. Who can consume Web API?
A large range of clients such as browsers, mobile devices, iPhone, etc., include or
consume web API. It is also good for using along native applications that require web
services but not SOAP support. It can also be consumed by any client that supports
HTTP verbs such as GET, DELETE, POST, PUT.
Web API generally provides greater flexibility in terms of handling errors. Exception
handling is a technique that is used to handle run-time errors in application code. One
can use HttpResponseException, HttpError, Exception filters, register exception filters,
Exception handlers to handle errors. Exception filter can be used to identify unhandled
exceptions on actions or controllers, exception handlers can be used to identify any type
of unhandled exception application-wide, and HttpResponseException can be used
when there is the possibility of an exception.
18. What is MVC? Write difference between MVC and Web API?
MVC (Model, View, and Controller) is basically an application design model that
comprises three interconnect parts I.e., model, view, and controller. It allows coders to
factor out different components of the application and update them more easily. It is
mostly used for developing model user interfaces. Its main purpose is to display
patterns in structure for keeping display and data separate to enable both of them to
change without affecting others.
ASP stands for Active server pages. ASP.NET is an updated version of legacy ASP. It is a
framework that is used for developing HTTP services to provide responses to client
requests. It can be accessed in different applications on different platforms. It is
provided by Microsoft open-source technology for developing and consuming HTTP-
based services on top of .NET Framework. It is very easy to build HTTP services using
ASP.NET Web API. These services can be used by different clients as given below:
Desktop Applications
Mobile Applications
IOTs
Browsers
Some of the core advantages of using ASP.NET Web API are given below:
21. What are new features used in ASP.NET Web API 2.0
ASP.NET Web API includes a number of new exciting features as given below:
Attribute Routing
CORS (Cross-Origin Resource Sharing)
OWIN (Open Web Interface for .NET) self-hosting
IHttpActionResult
Web API OData
It is used to set response values such as header and status control. It simply allows us to
work with HTTP protocol. It represents HTTP response messages that encapsulate data
and status code.
// GetEmployee action
public HttpResponseMessage GetEmployee(int id)
{
Employee emp = EmployeeContext.Employees.Where(e => e.Id ==
id).FirstOrDefault();
if (emp != null)
{
return Request.CreateResponse<Employee>(HttpStatusCode.OK,
emp);
} else
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound,
"Employee Not Found");
}
}
ApiController: It is used to return data that is arranged in series and then sent to the
client.
24. What do you mean by Caching and What are its types?
Advantages of Caching:
It is considered the best solution to ensure that data is served where it is needed
to be served that too at a high level of efficiency which is best for both client and server.
It delivers web objects faster to the end-user.
It reduces load time on the website server.
It leads to faster execution of any process.
It decreases network costs.
Types of Caching:
There are basically three types of caching as given below:
Page Caching
Data Caching
Fragment Caching
No, it's not true that ASP.NET Web API has replaced WCF. WCF was generally developed
to develop SOAP-based services. ASP.NET Web API is a new way to develop non-SOAP-
based services such as XML, JSON, etc. WCF is still considered a better choice if one has
their service using HTTP as the transport and they want to move to some other
transport like TCP, NetTCP, MSMQ, etc. WCF also allows one-way communication or
duplex communication.
26. What are the main return types supported in ASP. Net Web API?
HttpResponseMessage
IHttpActionResult
Void
Other types such as string, int, etc.
Routing is the most important part of ASP.NET Web API. Routing is a way how Web API
matches a URI to an action. It is basically a process that decides which action and
controller should be called. The controller is basically a class that handles all HTTP
requests. All public methods of controllers are basically known as action methods or just
actions. Whenever a Web API framework receives any type of request, it routes that
request to action.
There are basically two ways to implement routing in Web API as given below:
Convention-based routing: Web API supports convention-based routing. In this type
of routing, Web API uses route templates to select which controller and action method
to execute.
Attribute-based routing: Web API 2 generally supports a new type of routing known as
attribute routing. As the name suggests, it uses attributes to define routes. It is the
ability to add routes to the route table via attributes.
28. HOW to secure ASP.NET Web API?
Web API has become key to programming web-based interactions. It can be accessed
by anyone who knows the URL. Therefore, they have become targets for hackers. One
needs to secure Web API by controlling Web API and by deciding who can and who
cannot have access to Web API. There are basically two ways or techniques that make
our Web API more secure.
Authentication: It is a process that helps to identify and check users by their credentials
such as password, username, etc. To have access to the web API, firstly user credentials
are needed to be passed in the request header. If user credentials are not passed into
the request header, then the server returns 401 status code (unauthorized). The best
authentication to be used is OAuth 2.0.
Authorization: It is a process that helps to decide whether or not a user has access to
perform an action. Authorization filters are used to implement authorization.
Exception filter is generally used to handle all unhandled exceptions that are generated
in web API. It implements IExceptionFilters interface. It is the easiest and most flexible to
implement. This filter is executed whenever the controller method throws any
unhandled exception at any stage that is not an HttpResponseExecption exception.
.NET Framework 4.0 generally supports the first version of ASP.NET Web API. After
that, .NET Framework 4.5 supports the latest version of web API i.e., ASP.NET Web API 2.
It is considered as the main class that includes different properties with help of which
one can override the default behavior of Web API.
No, we cannot return the view from the ASP.NET Web API method. ASP.NET web API
develops HTTP services that provide raw data or information. ApiController in ASP.NET
MVC application only renders data that is serialized and sent to the client. One can use a
controller to provide normal views.
HTTP GET: This method is used to get information or data from a respective server at a
specified URL.
Example:
GET/RegisterStudent.asp?user=value1&pass=value2
HTTP POST: This method is used to send data or information to respective servers.
Example:
POST/RegisterStudent.asp HTTP/1.1
Host: www.guru99.com
user=value1&pass=value2
CORS (Cross-Origin Resource Sharing) is basically a mechanism that allows one to make
requests from one website to another website in a browser that is normally not allowed
by another policy called SOP (Same Origin Policy). It supports secure cross-origin
requests and data transfers among clients or browsers and servers. Here, cross-origin
request means requests coming from different origins. CORS simply resolves the same-
origin restriction for JavaScript. One can enable CORS for web API using the respective
web API package or OWIN middleware.
Page.Validate()
Context keys, documents keys, or anything that initiates API to hit the exact end-point
are few parameters that one can pass in the URL to define the complete end-point.
Using Web API tools like Fiddler, we can perform unit testing in Web API. Fiddler is
basically a free debugging proxy for any browser that can be used to compose and
execute various HTTP requests to Web API and check HTTP response. It is simply used
for testing restful web services. It allows one to inspect and check both incoming and
outgoing data to monitor and modify requests and responses before the browser
receives them. Below is given some setting that is needed to be done fiddler:
Fiddler – Compose Tab -> Enter Request Headers -> Enter Request Body and then
execute.
Name the component of HTTP response that contains metadata for the HTTP Response
message as key-value pairs?
Status/Response
Http Version
Response Header
Response Body
2.
Type of Data
Shape of Data
Html Content
Collection of Data
4.
ActionResult() is _
Concrete Class
Abstract Class
Both A and B
None of the above
5.
ViewResult() is _
Abstract Class
Concrete Class
Both A and B
None of the above
6.
File Access
AdRotator
LinkCounter
Counter
7.
Which of the following file extensions is used for ASP.NET Web API?
.ASPX
.ASP
.Web
None the of above
8.
Web forms are inherited from which of the following base classes?
Page Class
Session Class
Master Page
None the of above
9.
UDP
TCP
HTTP
None of the above
1. What is ASP.NET Web API?
ASP.NET Web API is a framework provided by Microsoft with which we can easily build
HTTP services that can reach a broad of clients, including browsers, mobile, IoT
devices, etc. ASP.NET Web API provides an ideal platform for building RESTful
applications on the .NET Framework.