Loading

ASP.NET Web API

ASP.NET Web API Test Questions and Answers. The Complete ASP.NET Web API Developer Course 2023 [Videos].

In this article, I am going to discuss the most frequently asked ASP.NET Web API Interview Questions and Answers. In the next article, I am going to discuss the Experienced ASP.NET Web API Interview questions and answers. Let start the ASP.NET Web API Interview Questions and Answers discussion with the most basic question that asked in almost all interviews i.e. What is ASP.NET Web API.

What is ASP.NET Web API?

The term API stands for Application Programming Interface. ASP.NET Web API is a framework that makes it easy to build Web APIs, i.e. HTTP based services on top of the .NET Framework. ASP.NET Web API is an ideal platform for building Restful services. These services can then be consumed by a broad range of clients like

  1. Browsers
  2. Mobile applications
  3. Desktop applications
  4. IOTs
What is the Rest?

REST stands for Representational State Transfer. This is an architectural pattern used for exchanging data over a distributed environment. In Rest, there is something called Client and Server, and the data can be exchanged between the client and server over a distributed environment. Distributed environment means the client can be on any platform like Java, .NET, PHP, etc. and the server can also be on any platform like Java, .NET, PHP, etc. The REST architectural pattern treats each service as a resource and a client can access those resources by using HTTP Protocol methods such as GET, POST, PUT, PATCH, and DELETE.

What is Not Rest?
  1. It is not a protocol
  2. It is not a standard
  3. It is not a replacement for SOAP
What are the RESTful services?

If you are preparing for ASP.NET Web API Interviews then definitely you have to prepare this Web API Interview QuestionREST stands for Representational State Transfer. The REST was first introduced in the year 2000 by Roy Fielding as part of his doctoral dissertation. REST is an architectural pattern for exchanging data over a distributed environment. REST architectural pattern treats each service as a resource and a client can access these resources by using HTTP protocol methods such as GET, POST, PUT, PATCH, and DELETE. The REST architectural pattern specifies a set of constraints that a system should be adhered to. Here are the REST constraints.

1. Client-Server Constraint:

This is the first constraint. 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 applications and server applications should be developed separately without any dependency on each other. A client should only know resource URIs and thats all. Severs and clients may also be replaced and developed independently as long as the interface between them is not altered.

2. Stateless Constraint:

The next constraint is the Stateless Constraint. The stateless constraint specifies that the communication between the client and the server must be stateless between requests. That means the server should not be storing anything on the server related to the client. The request from the client should contain all the necessary information so that the server can identify the client and can process that request. This ensures that each request can be treated independently by the server.

3. Cacheable Constraint:

In real-time applications, some data provided by the server is not changed that frequently like the list of Countries, the list of States,  the list of cities, and some master data. The Cacheable Constraint says that let the client know how long this data is good for so that the client does not have to come back to the server for that data over and over again.

4. Uniform Interface Constraint:

The Uniform Interface Constraint defines an interface between the client and the server. To understand the uniform interface constraint, first, we need to understand what a resource is and the HTTP verbs such as GET, PUT, POST, PATCH, and DELETE. In the context of a RESTFUL Service, resources typically represent data entities. The Product, Employee, Customer, Country, State, City, etc. are all resources. The HTTP verb (GET, PUT, POST, PATCH, and DELETE) that is sent with each request tells the API what to do with the resource. Each resource is identified by a specific URI (Uniform Resource Identifier).

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

What are the differences between REST and SOAP?

It is one of the most frequently asked Web API Interview Questions in Web API Interviews.

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 rather 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 that 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 Methods such as GET, POST, PUT, PATCH, and DELETE  to perform CRUD operations.
  6. SOAP uses interfaces and named operations (i.e. Service Contract and Operation Contract) to expose the service whereas to expose resources (service) REST uses URI and methods like (GET, PUT, POST, PATCH, and DELETE).
  7. SOAP Performance is slow as compared to REST.
What are the Differences between WCF and Web API? When to choose one over the other?

The first important point is, we can use both WCF and ASP.NET Web API to develop restful services. In fact, WCF comes first, and then ASP.NET Web API/

WCF (Windows Communication Foundation) is one of the choices available in the .NET Framework for developing both SOAP and REST services. The problem with WCF is that a lot of configuration is required to turn a WCF service (SOAP service) into a REST service. So the more natural choice for developing REST services is ASP.NET Web API In fact, ASP.NET Web API is specifically designed for this purpose i.e. for developing Restful Services.

WCF is more suitable for developing services that are transport/protocol independent. For example, our requirement is to build one service that can be consumed by 2 different clients – Lets say, a Java client and a .NET client. Java client wants the transport protocol to be HTTP and message format to be XML for interoperability, whereas the .NET client expects the protocol to be TCP and the message format to be binary for performance. For this scenario, WCF is the right choice. What we need to do here is, create a single WCF service, and then configure 2 endpoints one for each client (i.e. one for the Java client and the other one for the .NET client).

There is nothing wrong to use WCF to develop RESTFUL services. Its just that its a bit more complex and configuration can be a headache. So, if you are stuck with .NET Framework 3.5 or you have an existing SOAP service that you must support as well as you want to add REST to reach more clients, then you need to go for WCF. 

If you dont have the limitation of .NET Framework 3.5 and you want to create a brand new restful service then go with ASP.NET Web API.

WCF
  1. It is a framework used for developing SOA (service-oriented applications). 
  2. WCF can only be consumed by clients, which can understand XML. WCF supports protocols like – HTTP, TCP, Named Pipes, etc.
ASP.NET Web API
  1. It is a framework that helps us to develop HTTP Based services i.e. Restful Services.
  2. Web API is an open-source platform.
  3. It supports most of the MVC features which keep Web API over WCF.
When do we need to choose ASP.NET Web API?

Today, a web-based application is not enough to reach its customers. Now a day, people have become very smart; they are using iPhones, mobiles, tablets, etc. devices in their daily life. These devices are having a lot of apps that actually make their life easy. Actually, we are moving from the web towards the apps world.

So, if we like to expose our service data to the browsers as well as to all these modern device apps in a fast, simple, and secure way, then we should have an API that is compatible with browsers as well as with all these modern devices.

The ASP.NET WEB API is a great framework for building HTTP services i.e. Restful Services that can be consumed by a broad range of clients including browsers, mobiles, iPhones, and tablets, etc.  

What is the difference between WCF and WEB API and WCF REST and Web Service?

The .NET framework has a number of technologies that allow us to create HTTP services such as Web Service, WCF, WCF Rest, and now WEB API. Following are the differences between these four:

Web Service:
  1. The Web Service is based on SOAP and it returns the data in XML format.
  2. It supports only the HTTP protocol.
  3. Web Service is not open-source but it can be consumed by any client who understands XML.
  4. It can only be hosted on IIS.
WCF:
  1. WCF is also based on SOAP and it also returns the data in the form of XML.
  2. Unlike Web service, WCF supports different types of protocols (transport protocol) such as TCP, Named Pipes, HTTP, HTTPS, and MSMQ.
  3. The main problem with the WCF service it required lots of configuration which is a headache for a developer.
  4. Like Web Service, WCF is also not open source but it can be consumed by any client who understands XML.
  5. WCF can be host within the application or on IIS or using Windows service.
WCF Rest:
  1. To use a WCF service as WCF Rest service we have to enable webHttpBindings.
  2. WCF Rest supports the HTTP verbs such as GET and POST by using the [WebGet] and [WebInvoke] attributes respectively.
  3. To use other HTTP verbs you have to do some configuration in the IIS so that it will accept the request of that particular verb on the .svc file
  4. It supports different data formats such as XML, JSON, and Atom format.
WEB API:
  1. The Web API Framework is a new framework that is basically used for developing HTTP-based services in an easy and simple way.
  2. Unlike WCF Rest Service, it uses full HTTP features such as URIs, request/response headers, caching, versioning, and various data formats.
  3. The ASP.NET Web API also supports most of the MVC features such as routing, controllers, actions, filter, model binders, IOC container, dependency injection, unit testing which makes it more simple and robust.
  4. WEB API Services can be hosted on IIS or within the application
  5. The Responses in Web API Services are formatted by MediaTypeFormatter into JSON, XML, or any custom format you want.
What are the advantages of using REST in ASP.NET Web API?

The REST uses fewer data transfers between client and server which makes REST ideal for use in mobile apps. The ASP.NET Web API supports HTTP Protocols and thereby it reintroduces the old way of HTTP verbs for communication.

What are the Differences between WCF Rest and Web API?
WCF Rest
  1. We need to use WebHttpBinding to be enabled for WCF Rest.
  2. For each method, there have to be attributes like – “WebGet” and “WebInvoke”
Web API
  1. Unlike WCF Rest, we can use full features of HTTP in Web API.
  2. Web API can be hosted in IIS or in an application.
What are the advantages of using ASP.NET Web API?

Using ASP.NET Web API has a number of advantages, but the core advantages are as follows:

  1. It supports all the HTTP features and allows all standard HTTP verbs such as GET, POST, PUT, PATCH, and DELETE to perform CRUD operations
  2. Complete support for Routing which is good for SEO.
  3. It supports content negotiation i.e. as per the client request, the server sends the response in that format (if possible). The Response generated in JSON or XML format using MediaTypeFormatter
  4. It has the ability to be hosted in IIS as well as self-host outside of IIS
  5. Supports Model binding and Validation
What new features are introduced in ASP.NET Web API 2.0?

The new features introduced in ASP.NET Web API framework v2.0 are as follows:

  1. Attribute Routing
  2. External Authentication (third party authentication)
  3. CORS (Cross-Origin Resource Sharing)
  4. OWIN (Open Web Interface for .NET) Self Hosting
  5. IHttpActionResult
What are the differences between ASP.NET MVC and ASP.NET Web API?

This is one of the frequently asked ASP.NET Web API Interview Questions. The following are some of the differences between ASP.NET MVC and ASP.NET Web API 

MVC
  1. ASP.NET MVC Framework basically used to create a web application by following the MVC (Model-View-Controller) design pattern, in which we can build web pages.
  2. The action method can return both data and view. It only returns data in JSON format using JsonResult
  3. All requests are mapped to the respective action based on the action method names.
  4. Content-negotiation not supported.
Web API
  1. ASP.NET Web API Framework is used to develop Restful services that can be consumed by different clients.
  2. Web API returns data in a particular format like JSON, XML, or any other based upon the Accept header in the request. It does not return the view to the client.
  3. All requests are mapped to actions using HTTP verbs.
  4. Content-negotiation supported.

Note: We can mix Web API and MVC controller in a single project to handle advanced AJAX requests which may return data in JSON, XML, or any other format, and building a full-blown HTTP service. Typically, this will be called Web API self-hosting. 

Is it true that ASP.NET Web API has replaced WCF?

Its not true. Its a misconception that ASP.NET Web API has replaced WCF. ASP.NET Web API is just another way of building non-SOAP-based services, for example, plain XML or JSON string, etc.

Yes, ASP.NET Web API has some added advantages like utilizing the full features of HTTP (HTTP Built-in Header and HTTP Verbs such as GET, POST, PUT, PATCH, and DELETE, HTTP Status codes such as 500, 200, 404, etc.) and reaching more clients such as mobile devices, Tables, IoTs, Browsers, etc. But WCF is still a good choice for the following scenarios:

  1. If you want to develop transport protocol-oriented services other than HTTP, such as TCP, UDP, or Named Pipes, etc. If HTTP only then go with Web API.
  2. Message Queuing scenario using MSMQ
  3. One-way communication or Duplex communication
Explain Media Formatters in Web API 2

It is one of the frequently asked ASP.NET Web API Interview Questions. As we know that Web API Framework handles the data in JSON or XML formats (or any other formats) based on the Accept and Content-Type header values. But do you know how does the ASP.NET Web API Framework handles these different formats? The answer is by using Media Type Formatters.

The Media-Type Formatters are classes that are responsible for serializing the request/response data. Based on the accept and content-type header values the ASP.NET Web API Framework can understand the request data format and sends the data in the format which the client expects.

Technically MediaTypeFormatter is an abstract class from which JsonMediaTypeFormatter and XmlMediaTypeFormatter classes inherit from. JsonMediaTypeFormatter handles JSON data and XmlMediaTypeFormatter handles XML data.

How to return JSON from Web API Service irrespective of the Accept header value?

To do so, we need to remove the XmlFormatter from the Register() method of WebApiConfig.cs file which is present inside the App_Start folder. To remove XmlFormatter include the following piece of code that will completely remove XmlFormatter which forces ASP.NET Web API to always return data in JSON format irrespective of the Accept header value in the client request. Use this technique when you want your service to support only JSON and not XML.

config.Formatters.Remove(config.Formatters.XmlFormatter);
With this change in place inside the Register method of the WebApiConfig class, irrespective of the Accept header value (application/xml or application/json), the Web API service is always going to return the data in JSON format.

How to return XML data from Web API Service irrespective of the Accept header value?

To do so, we need to remove the JsonFormatter from the Register() method of WebApiConfig.cs file which is present inside the App_Start folder. To remove we can include the following piece of code that will completely remove JsonFormatter which forces ASP.NET Web API to always return XML irrespective of the Accept header value in the client request. Use this technique when you want your service to support only XML and not JSON.

config.Formatters.Remove(config.Formatters.JsonFormatter);
With this change in place inside the Register method of the WebApiConfig class, irrespective of the Accept header value (application/xml or application/json), the Web API service is always going to return the data in XML format.

How to return JSON instead of XML from Web API Service when a request is made from the browser?

So here is what we want the service to do

  1. When a request is issued from the browser, the ASP.NET Web API service should return the data in JSON format instead of XML. 
  2. When a request is issued from a tool like a fiddler or Postman, the Accept header value should be respected. That means if the Accept header value is set to application/xml the service should return XML and if the Accept header value is set to application/json the service should return the data in JSON.

There are 2 ways to achieve this

Approach2: 

Include the following line in Register() method of WebApiConfig.cs file in the App_Start folder. This tells ASP.NET Web API to use JsonFormatter when a request is made for text/html which is the default for most browsers. The problem with this approach is that the Content-Type header of the response is set to text/html which is misleading.

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue(“text/html”));

Approach2: 

Include the following class in WebApiConfig.cs file in the App_Start folder. 

Register Formatter: 

Place the following line in Register() method of WebApiConfig.cs file in App_Start folder

ASP.NET WEB API Interview Questions and Answers

config.Formatters.Add(new CustomJsonFormatter());
With these 2 changes, when a request is issued from the browser you will get JSON formatted data and the Content-Type header of the response is also set to application/json. If you are using tools like a fiddler and if you set Accept header to application/xml you will still get XML formatted data.


See All

Comments (724 Comments)

Submit Your Comment

See All Posts

Related Posts

ASP.NET Web API / Blog

What is ASP.NET Web API Application?

In this ASP.NET Web API Tutorials series, I covered all the features of ASP.NET Web API. You will learn from basic to advance level features of ASP.NET Web API. The term API stands for “Application Programming Interface” and ASP.NET Web API is a framework provided by Microsoft which makes it easy to build Web APIs, i.e. it is used to develop HTTP-based web services on the top of .NET Framework.
3-Feb-2022 /34 /724

ASP.NET Web API / Blog

How to creat ASP.NET Web API Application using Visual Studio?

In this article, I am going to discuss the step-by-step procedure for Creating ASP.NET Web API Application. Please read our previous article before proceeding to this article where we gave an overview of the ASP.NET Web API framework. As part of this article, we ate going to discuss the following pointers.
3-Feb-2022 /34 /724

ASP.NET Web API / Blog

How to add Swagger in Web API Application?

In this article, I am going to discuss how to add Swagger in Web API Application to document and test restful Web API services. Please read our previous article where we discussed How to Create an ASP.NET Web API Application step by step before proceeding to this article as we are going to work with the same example. As part of this article, we are going to discuss the following pointers.
3-Feb-2022 /34 /724