Top 20 ASP.NET Web API Interview Questions

 Print   10 min read  
04 Sep 2022
281K Views

ASP.NET Web API is a framework provided by the 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.

  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.

  2. What is the difference between ASP.NET Web API and WCF?

    Web API is a Framework to build HTTP Services that can reach a board of clients, including browsers, mobile, IoT Devices, etc. and provided an ideal platform for building RESTful applications. It is limited to HTTP based services. ASP.NET framework ships out with the .NET framework and is Open Source. WCF i.e. Windows Communication Foundation is a framework used for building Service Oriented applications (SOA) and supports multiple transport protocol like HTTP, TCP, MSMQ, etc. It supports multiple protocols like HTTP, TCP, Named Pipes, MSMQ, etc. WCF ships out with the .NET Framework. Both Web API and WCF can be self-hosted or can be hosted on the IIS Server.

  3. When to prefer ASP.NET Web API over WCF?

    It totally depends upon the requirement. Choose ASP.NET Web API is you want only HTTP based services only as Web API is a lightweight architecture and is good for the devices which have limited bandwidth. We can also create the REST services with the WCF, but that requires lots of configuration. In case, if you want a service that should support multiple transport protocol like HTTP, UDP, TCP, etc. then WCF will be a better option.

  4. Which .NET Framework supports ASP.NET Web API?

    First Version of ASP.NET Web API is introduced in .NET Framework 4. After that, all the later versions of the .NET Framework supports the ASP.NET Web API.

  5. Can we consume ASP.NET Web API in applications created using other than .NET?

    Yes, we can consume ASP.NET Web API in the applications created using another language than .NET but that application must have access/supports to the HTTP protocol.

  6. What is the difference between ASP.NET MVC application and ASP.NET Web API application?

    ASP.NET MVC is used to create a web application which returns both data as well as View whereas Web API is used to create HTTP based Services which only returns data not view. In an ASP.NET MVC application, requests are mapped to Action Methods whereas in the ASP.NET Web API request is mapped to Action based on the Action Verbs.

  7. What are the RESTful Services?

    REST stands for the Representational State Transfer. This term is coined by the Roy Fielding in 2000. RESTful is an Architectural style for creating loosely couple applications over the HTTP. In order to make API to be RESTful, it has to adhere the around 6 constraints that are mentioned below:

    1. Client and Server Separation: Server and Clients are clearly isolated in the RESTful services.

    2. Stateless: REST Architecture is based on the HTTP Protocol and the server response can be cached by the clients, but no client context would be stored on the server.

    3. Uniform Interface: Allows a limited set of operation defined using the HTTP Verbs. For eg: GET, PUT, POST, Delete etc.

    4. Cacheable: RESTful architecture allows the response to be cached or not. Caching improves performance and scalability.

    5. Code-On-Demand

    6. Layered System

  8. What are the new features introduced in ASP.NET Web API 2.0?

    The following features have been introduced in ASP.NET Web API 2.0:

    • Attribute Routing

    • CORS (Cross-Origin Resource Sharing)

    • OWIN (Open Web Interface for .NET) self-hosting

    • IHttpActionResult

    • Web API OData etc.

  9. Can we return View from Web API?

    No, Web API does not return View but they return the data. APIController is meant for returning the data. So, if you need to return a view from the controller class, then make sure to use or inherit the Controller class.

  10. Does ASP.NET Web API replace the WCF?

    No, ASP.NET Web API didn’t replace WCF Service as it is only used for creating RESTful Service i.e. non-SOAP based service.

  11. What is Request Verbs or HTTP Verbs?

    In RESTful service, we can perform all types of CRUD (Create, Read, Update, Delete) Operation. In REST architecture, it is suggested to have a specific Request Verb or HTTP verb on the specific type of the call made to the server. Popular Request Verbs or HTTP Verbs are mentioned below:

    1. HTTP Get: Used to get or retrieve the resource or information only.

    2. HTTP Post: Used to create a new resource on the collection of resources.

    3. HTTP Put: Used to update the existing Response

    4. HTTP Delete: Used to Delete an existing resource.

  12. What are HTTP Status Codes?

    HTTP Status Code Is 3-digit integer in which the first digit of the Status-Code defines the class of response. Response Header of each API response contains the HTTP Status Code. HTTP Status Codes are grouped into five categories based upon the first number.

    S. No.
    HTTP Status Code
    Description
    1.
    1XX
    Informational
    2.
    2XX
    Success
    3.
    3XX
    Redirection
    4.
    4XX
    Client-Side Error
    5.
    5XX
    Server-Side Error
    Table: HTTP Status Code with Description

    Some of the commonly seen HTTP Status Codes are: 200 (Request is Ok), 201 (Created), 202 (Accepted), 204 (No Content), 301 (Moved Permanently), 400 (Bad Request), 401 (Unauthorized), 403 (Forbidden), 404 (Not Found), 500 (Internal Server Error), 502 (Bad Gateway), 503 (Service Unavailable) etc.

  13. What is Parameter Binding in ASP.NET Web API?

    When Web API calls a method on a controller, it must set the values for the parameters, this particular process is known as Parameter Binding. By Default, Web API uses the below rules in order to bind the parameter:

    • FromUri: If the parameter is of “Simple” type, then Web API tries to get the value from the URI. Simple Type includes.Net Primitive type like int, double, etc., DateTime, TimeSpan, GUID, string, any type which can be converted from the string type.

    • FromBody: If the parameter is of “Complex” type, then Web API will try to bind the values from the message body.

  14. What is Content Negotiation in Web API?

    Content Negotiation is the process of selecting the best representation for a given response when there are multiple representations available. Two main headers which are responsible for the Content Negotiation are:

    • Content-Type

    • Accept

      The content-type header tells the server about the data, the server is going to receive from the client whereas another way to use Accept-Header, which tells the format of data requested by the Client from a server. In the below example, we requested the data from the server in JSON format.

  15. What is Media-Type Formatter in ASP.NET Web API?

    Media-Type formatter is an abstract class from which JsonMediaTypeFormatter (handle JSON format) and XmlMediaTypeFormatter (handle XML format) class derived from. Media-Type formatter are classes responsible for serializing the response data in the format that the client asked for.

  16. What is the use of Authorize Attribute?

    Web API provided a built-in authorization filter, i.e. Authorize Attribute. This filter checks whether the user is authenticated or not. If not, the user will see 401 Unauthorized HTTP Status Code.

  17. What is Basic HTTP Authentication?

    Basic HTTP Authentication is a mechanism, where the user is authenticated through the service in which the client pass username and password in the HTTP Authorization request headers. The credentials are formatted as the string “username:password”, based encoded.

  18. How Web API Routes HTTP request to the Controller ASP.NET MVC?

    In ASP.NET Web API, HTTP request maps to the controller. In order to determine which action is to invoke, the Web API framework uses a routing table.

  19. In How many ways we can do Web API Versioning?

    We can do Web API Versioning in the following ways:

    1. URI

    2. Query String Parameter

    3. Custom Header Parameter

    4. Accept Header Parameter

  20. What is Exception handling?

    Exception handling is a technique to handle runtime error in the application code. In multiple ways we can handle the error in ASP.NET Web API, some of them are listed below:

    1. HttpResponseException

    2. HttpError

    3. Exception Filters etc.

Summary

I hope these questions and answers will help you to crack your Web API Interview. These interview Questions have been taken from our new released eBook ASP.NET Web API Interview Questions.

This eBook has been written to make you confident in Web API with a solid foundation. It's would be equally helpful in building REST API using ASP.NET Web API and integrating it with your real projects.

Buy this eBook at a Discounted Price!

ASP.NET Web API Interview Questions eBook

Learn to Crack Your Technical Interview

Accept cookies & close this