0 ratings0% found this document useful (0 votes) 115 views2 pagesMost Asked Web API Interview Questions 2023 Javatpoint
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
Followingis 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?
STACK IN DS
C PROGRAM TO IMPLEMENT
STACK
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
For example:
°
Web API provides us the ability to create nom-SOAP-based HTTP services.
°
‘We can easily use it with HTTP verbs for Create, Read, Update and Delete operations,
°
It is very lightweight, and its lightweight architecture makes it ideal for small bandwidth devices such
as smartphones,
°
Itis based on HTTP and easy to define, expose and consume in a REST-ful way.
°
It provides an ability to select response output in either JSON or XML and support for Open Data
(OData) protocol.
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 WCE, but
two main reasons make users choose Web API instead of RESTful services
© Web API increases the TDD (Test Data-Driven) approach in the development of RESTful services.
© If we want to develop RESTful services in WCF, you surely need many config settings, URI templates,
contracts & endpoints,
4) Is it true that ASP.NET Web API has replaced WCF?
No, itis not true et all that ASP.NET Web AP! has replaced WCF. ASP.NET Web API is another way to build
non-SOAP-based services, ie, plain XML or ISON 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 nezd to use HTTP as your transport, Butif you want to use a different protocel 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
© Content Negotiation
© Self-Hosting
© CData
© Filters
© Routing
© Model Bindings
7) What are the differences between Web API and MVC?
Following are the main differences between Web API and MYC (Madel View Controller)
Mvc Web API
MVC is an application design model used to create web Web API is used to create HTTP
applications with a front-end and controls and back-end services to interact with a variety of
processes clients
We can use the MVC to create web apps with user interfaces. We use Web API to develop HTTP
services.
In the MVC design pattern, the web application returns a view Web API returns the data only.
as well as data
MVC can only map requests to action methods, \With Web API, we can map all requests
to actions using HTTP verbs.
MVC can return data only in JSON format. Web API can return data in JSON and
XML, among other formats
8) Which .NET framework supports Web API?
The INET 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:
@x
‘ q Oquee
preciso para
aaa conhecer
i
Deus?
SuaEscolna.com
© Void: Itis used to return empty content,
© HttpResponseMessage: It is used to convert the respanse to an HTTP message,
© IHttpActionResult: Its used to internally call ExecuteAsync to create an HttpResponseMessage,
© Other types: It helps us to write the serialized return value into the response body,
10) Which open-source library is supported by Web API for JSON serialization?
Web API uses ISON.NET library for ISON 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,
For example:
Routes MapHttpRoute(
Name: "ExampleWebAPIRoute",
routeTemplate: “api/{controller}/{id)
defaults: new {id = RouteParameter Optional}
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 musthave access/support to the HTTP protocol.
14) What do you understand by SOAP?
The full forrn 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 HITP 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 ky performing the following three simple steps:
© First, create a Web API Controller,
© Now, add a routing table to the Application_Start method of Global. sax
© Now, we can make a jQuery AJAX Call to Web API method and get the data
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:
© Glientand Server Separation: Clients and Servers are isolated in the RESTful services.
© Stateless: REST architecture is based on the HTTP Protocol, and the clients can cache the server
response, but no client context would be stored on the server,
© Uniform Interface: Itis used to allow a limited set of operations defined using the HTTP Verbs. For
example, GET, PUT, POST, Delete, ete.
© Cacheable: RESTful architecture allows the response to be cached or not. Caching improves
performance and scalability.
© Code-On-Demand
© Layered System
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. Excepticn filters implement the “IException Filter” 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 INET 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:
© FromUri: If the parameter is of "Simple" type, the Web API tries to get the URI value. Simple Type
includes.Net Primitive type like int double, etc, DateTime, TimeSpan, GUID, string, any type which can
convert from the string type.
© FromBody: If the parameter is of "Complex" type, the Web API only binds the values from the
message body.
22) How can you register exception filter globally?
We can easily register exception filter globally by using the following code
GlobalConfiguration.Configuration Filters Add(n ew
MyTestCustomerStore NotlmplExceptionFilterAttributep);
23) What is the difference between REST and RESTful?
Difference between REST and RESTful:
REST RESTful
The full form of REST is The term RESTful is It is a completely It mainly focuses on system
Representational State written by applying REST new aspect of resources and how the
Transfer. architectural concepts writing a web resource's state should be
called RESTful services app. transported over HTTP
protocol.
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:
© Content-Type: The content-type header is used to specify the server about the data; the server will
receive from the dient
© Accept: Another way for Content Negotiation is to use Accept-Header. Its used to specify the format
of data requested by the client from a server
25) Give an example to specify Web API Routing?
Following is an example of Web API Routing:
ConfigRoutes MapHttpRoute(
name: "MyRoute,//route name
routeTemplate: “api/{controller}/{action}/tid)",//as you can see "API" is at the beginning,
defaults: new {id = RouteParameter.Optional }
)
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, IsonMeciaTypeFormatter (which is used to handle JSON
format) and XmIMediaypeFormatter (which is used to handle XML format), are derived from the Media-
Type formatter,
The MediaType formatter classes are also responsible for serlalizing 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.07
Following are the latest new features of ASP.NET Web API framework v2.0:
© Attribute Routing
© Open Web Interface NET
© Cross-Origin Resource Sharing
© External Authentication
© HttpActionResult
© Web API OData
29) How can you restrict access methods to specific HTTP verbs in Web API?
Itis 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,
For example,
[HttpPost]
public void Method! (Class obj)
{
iMvrite logic here
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:
© Using ArrayList
© Using Newtonsoft array
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
ArrayList paramList = new ArrayList(;
Category c = new Category { Categoryld = 1, CategoryName ="MobilePhones"}
Product p = new Product { Productcode = 1, Name = "MotoG", Price = 15500, CategoryID = 1 };
paramListAdd¢c)
paramListAdd(p);
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 Weo 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 thraw the response body's errar info. Alang 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
GlobalConfiguration. Configuration Filters Add (new MyTestCustomerStore NotimplExceptionFilterAttribu
37) What do you understand by Exception handling in Web API?
Exceptian 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. Followingis a list of some of them
© HttpResponseException
© HttpError
© Exception Filters ete
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
© URI
°
Query String Parameter
°
Custom Header Parameter
°
AcceptHeader Parameter
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
clint can interact with the service by using the HTTP verbs,
41) What is the difference between MVC and Web API?
Differences between MYC and Web API:
MVC framework Web API
The MVC framework is used for developing applications WebAPI is used for developing HITP
that have a User Interface. services.
In the MVC framework, views are used for building User Other apps can also call the WebAPI
Interface. methods to fetch that data
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 ta consume them by any client. Even
portable devices like mobile devices can easily consume them, anditis 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
© GET: The GET method is used to retrieve the information from the respective server using a given
URI,
© HEAD: The HEAD method is the same as the GET method, but it is used to transfer the status line and
header section
© PUT: The PLIT method is used to update values, and it can replace all current resources with the
uploaded content
© POST: APOST methodis a request, and its used to send data to the respective server.
© DELETE: The DELETE method is used to delete or remove all current resources given by a URI
© OPTIONS: This method is used to describe the communication options for the target resource
© CONNECT: The CONNECT method is used to establish a tunnel to the server identified by a given
URI.
© TRACE: The TRACE method is used to perform a message loop-back test along the target resource
path.
7, Note: From the list of al the above methads, the GET, PUT, POST, and DELETE are the mast popular
methods. All methods are case-sensitive, and we must use them in upaercase
44) What do you understand by the HTTP Status Codes?
The Response Header of each API response consists of an HITP Status Code. The HITP 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
HTTP Status Status Meaning
Codes
x Informational It specifies that the request has been received and the process is
continuing.
2x Success, It specifies that the action was successfully received, understood,
and accepted,
Box Redirection It specifies that we have to take further actions to complete the
request
Ax ClientError It specifies that the request contains incorrect syntax or cannot be
fulfilled.
Six Server Error It specifies that the server failed to fulfil a valid request.
45) What are some commonly seen HTTP Status Codes?
Following is the list of some commonly seen HTTP Status Codes
© 200 (Request is Ck)
© 201 (Created)
© 202 (Accepted)
© 204 (No Content)
© 301 (Moved Permanently)
© 400 Bad Request)
© 401 (Unauthorized)
© 408 (Forbidden)
© 404 (Not Found)
© 500 (Internal Server Error)
© 502 (Bad Gateway)
© 508 (Service Unavailable) ete.
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." Ithas become the de facto standard for identifying content on the Internet.
For example, suppose we receive an email from 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:
XML JSON
The full form of XML is eXtensible Markup
Language. The full form of JSON is JavaScript
Object Notation,
It is a markup language like HTML and is JSON is often used when data is sent from a server
designed to store and transport data, to.a web page. Itis also used to store and transport
data.
XML doesn't do anything, butit can store data ina ISON is a. self-describing, easy-to-understand,
specific format. lightweight format for storing and transporting data
48) In which .NET framework the ASP .NET Web API was introduced?
The first version of ASP.NET Web API was introduced in INET 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:
WCF MYC Web API
WCF stands. for = Windows MVC stands for Model View Web API is an open-source
Communication Foundation. It is a Controller. Itis used to create platform and framework used to
framework that is used to build or a web application that create HTTP-based services that
develop service-oriented returns both data and view | only return data and not view.
applications. (HTML).
Itis designed to exchange standard MYC is used to facilitate the It supports most of the MYC
SOAP-based messages, easy rendering of HTML. features, which keep Web API over
WCF,
WCF can be consumed by clients MVC is used to create a web The Web API framework is used to
who can understand XML app in which we cen build generate HTTP services that cover
web pages more clients by generating raw
format data, for exarnple, plain
XML or JSON string.
WCF supports a variety of transport JSONIt will return Web API creates simple HTTP
protocols such as HTTP, TCP, JSONResult from the action services that render raw data.
Named Pipes or MSMQ, etc method.WCF ships out with the .NET All requests are mapped to Web API returns XML or ISON to
Framework the respective
methods.
action | the client,
WCF service is good for Message We can return a view from All requests are mapped to actions
Queue, duplex communication, MVC. In an ASP.NET MYC using HTTP verbs.
one-way messaging application,
mapped to Action Methods.
50) How can we unit test the Web API?
are
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 WebApiConfigcs 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
ISON formatirrespective of the Accept header value in the cliant request.
configFormatters Rem ove(configFormattersXmIFormatter)
52) What is the difference between Controller and ApiController?
Controller is used to rendering our normal views.
Example:
public class TweetsController : Controller {
1) GET: fIweets/
[HttpGet]
public ActionResult Index) {
return Json(Iwitter GetTweets0, JsonRequestBehavior AllowGet);
}
}
ApiController is used when we have only to return data that is serialized and sent to the client.
Example:
public class TweetsController : ApiController {
Uf GET: /Api/Tweets/
public List Get0 |
return Twitter. Getlweets0;
)
}
Interview Tips
Company Interview Questions & Procedure
Java Basics Interview Questons
Servlet Interview Questions
Spring Interview Questions
PL/SQL Interview Questions
Oracle Interview Questions
JQuery Interview Questions
Job/HR Interview Questions
JavaScript Interview Questions
Java OOPs Interview Questions
ISP Interview Questions
Hibernate Interview Questions
SQL Interview Questions
Ancroid Interview Questions
MySQL Interview Questions