In this session, we will see ASP.NET Web API Interview Questions that are asked during Interviews. ASP.NET Web API is very famous nowadays.

ASP.NET Web API Interview Questions:

Web API Interview Questions

1-What is a Web API?

Ans- ASP.NET Web API is a framework for building HTTP Services that can be consumed by a broader range of clients like browsers and mobile devices. ASP.NET Web API can be used to create non-soap based services like XML or JSON string etc. ASP.NET Web API has many other advantages like.

  1. Web API is used to create Non-Soap Service with the full support of HTTP.
  2. The Web API can be consumed by a broader range of clients like browsers and mobile devices.

2-What are the advantages of using ASP.NET Web API?

Ans- ASP.NET Web API has a number of advantages:

  1.  It works the HTTP way using the Standard Verb GET, POST, PUT DELETE etc.
  2. It has full support for routing.
  3.  The response can be 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. Support model binding and validations.
  6. Support for OData.

The Open Data Protocol (OData) is a data access protocol for the web. OData provides a uniform way to query and manipulate data sets through CRUD operations (create, read, update, and delete).

 3- What is REST?

Ans- REST stands for Representational State Transfer was first conceived by Roy Fielding. REST is an architectural pattern that uses HTTP as its underlying pattern. It is an approach to communications that can be used while developing Web Services.

4- What are the main Return types supported in Web API?

Ans- A Web API controller can return following types of values.

  1. void:  It will return empty content(204).
  2. HttpResponseMessage: Convert directly to an HTTP response message.
  3. IHttpResponseMessage: This will internally call the ExecuteSync method to create an HTTPResponseMessage then convert to an HTTP response message.
  4. Other return types: You can write the serialized value into the return body.

5- What is the biggest disadvantage of “Other Return Types” in Web API?

Ans- The biggest disadvantage of this approach is that you cannot directly return an error code like 404 error.

6- Explain Web API Routing?

Routing in ASP.NET Web API is a pattern matching system as we have in ASP.NET MVC. These Routes will get registered in Route Tables. Here is the sample code for Routing in ASP.NET Web API:

Routes.MapHttpRoute(
Name: "MyFirstWebAPIRoute",
routeTemplate: “api/{controller}/{id}
defaults: new { id = RouteParameter.Optional}
};

6- How can we handle errors in Web API?

Ans- Here are the number of classes that can be used to handle errors in Web API.

  1. HTTPResponse Exceptions.
  2. Exception Filters.
  3. Registering Exception filters.
  4. HTTPErrors.

7- What is the difference between MVC and Web API?

Ans-

MVC:

  1. MVC is used to create Web Apps where we can create web pages.
  2. For JSON it will return JSONResult from the action method.
  3. All requests are mapped to respective action methods.

Web API:

  1. Web API is used to create HTTP Services using HTTP Verbs.
  2. This returns JSON and XML data to the client.
  3. All requests are mapped to actions using HTTP verbs.

8- How can we restrict access to a methods with specific HTTP verb in Web API?

Ans- Attribute programming is used for this. Web API will support to restrict access of calling the method with specific HTTP verbs. We can define HTTP verb as attribute over the method as shown below:

[HttpPost]
public void PostEmployee(Employee objEmployee)
{
EmployeeRepository.AddEmployee(objEmployee); 
}

View More:

Conclusion:

I hope this session about ASP.NET Web API Interview Questions is helpful for you. You can comment in comment box for any technical questions. I would like to have your feedback and suggestions.

Thank You.

Leave a Comment