Web API Questions & Answers
1)
Explain
what is Web API?
The ASP.NET Web API is an extensible framework
for building HTTP based services that can be accessed in different applications
on different platforms such as web, windows, mobile etc. It works more-or-less
the same way as ASP.NET MVC web application except that it
sends data as a response instead of html view.
In simple words, it is a framework which helps you to
build/develop HTTP based REST services.
2)
Why
Web API is needed? Can we use RESTful services using WCF?
We can certainly build RESTful services using WCF. But,
there are two main reasons why people use Web API instead of RESTful services.
·
Web API increases TDD (Test Driven Development) approach in the
development of RESTful services.
·
If we want to develop RESTful services in WCF,
we surely need a lot of config settings, URI templates, contracts &
endpoints for developing RESTful services using WCF.
3)
Why
to choose Web API?
·
We can build simple, non-SOAP-based HTTP Services
·
The process of creating REST services using Web API is also
very easy.
·
It is based on HTTP and easy to define, expose and consume in
a RESTful way.
·
The Web API architecture is lightweight and ideal for devices that have
limited bandwidth like smartphones.
4)
Can
we claim that ASP.NET Web API has
replaced WCF?
It’s not at all fair to say that ASP.NET Web
API has replaced WCF. In reality, it is another way of building
non-SOAP based services, i.e., plain XML or JSON string.
5)
What
are the advantages of Web API?
Advantages of Web API are:
·
OData
·
Filters
·
Content Negotiation
·
Self-Hosting
·
Routing
·
Model Bindings.
6)
What
are the different HTTP verbs used in Web
API?
The important HTTP verbs used in Web API are Http Get, Put, Post and
Delete.
7)
What
are the different return types used in Web
API?
An action from Web API controller can return
following types:
·
HttpResponseMessage – It will convert the response to an HTTP
message.
·
IHttpActionResult – internally calls ExecuteAsync to create
an HttpResponseMessage
·
Void – It will return empty content
·
Other types – You can write the serialized return value into
the response body.
8)
Which
protocol does Web API support?
HTTP protocol.
9)
What
kind of service does Web API expose?
RESTful services.
10) In which version of .NET framework is Web API introduced for first time?
It was introduced in version 4.0 for the first time and available in 4.0
and above.
11) What is the open-source library used in Web API for JSON serialization?
It is Json.NET library for JSON serialization.
12) What is the default status code in Web API HTTP response for all uncaught
exceptions?
500 – Internal Server Error.
13) Can you explain Routing in Web API?
Web API routing
is similar to ASP.NET MVC Routing. It routes an incoming
HTTP request to a particular action method on a Web API controller. It is kind
of pattern matching just like in MVC.
All routes are registered in Route Table.
14) What is the benefit of using REST in Web API?
One of the very important benefit of REST APIs is that it has
a great deal of flexibility. Data is never tightly coupled to resources or methods,
so REST
can handle several kinds of calls, return different data formats and even
change structurally with the correct implementation of hypermedia.
Other benefit is that it is lightweight compared to SOAP and this makes it
ideal for using it in mobile applications where the internet bandwidth is
generally low.
15) What are the different ways to handle
errors in Web API?
Errors in Web API can be handled in various ways by using several classes
that are available in Web API. These classes are HttpError,
Exception Filters, HttpResponseException, Registering Exception Filters, and
Exception Handlers.
16) How to test your Web API url?
There are several open source tools available for testing the Web
API urls like Postman, Fidler etc.