In previous article, I have mentioned Postman and API Testing Interview questions with answers but now in this article, I have mentioned best 15+ C#/.NET Web API interview questions with answers, which a developer should know.

webapi-Interview-Questions-and-answers

1. What is an API?

An API (Application Programming Interface) is a software intermediary that enables two applications to communicate with each other. It comprises a number of subroutine definitions, logs, and tools for creating application software.

2. What is ASP.NET Web API?

ASP.NET Web api, helps us to create API in .NET framework, which then can be consumed in different applications on different platforms such as web, windows, mobile, etc. using HTTP protocol.

Using ASP.NET Web API, we can create non-SOAP based services like plain XML or JSON strings, etc.

3. What are the Advantages of Using ASP.NET Web API?

  • Centralization of business logic will lessen your efforts, making your work worthwhile. As a result, business information can be maintained with consistency.
  • These RESTful Web APIs are accessible by a variety of HTTP clients like Windows 7, Windows 8, Android, iPhone, iPad, WPF Client, Windows Phone 8, etc..
  • It is open-source.
  • It uses low bandwidth. (XML or JSON data). We can also pass HTML content if required.
  • The Web API Controller pattern is much similar to MVC Controller. Thus, it becomes easy to maintain and understand.
  • Web API is not part of the MVC Framework but it is part of Asp.net Framework. Therefore, no other dependencies are there for development and even deployment.
  • Easy to test, we can use application like Postman to test it.
  • Supports Model binding and Validation
  • Support for OData

4. What are main differences between API and Web Service?

Web Service:

  • It is a SOAP-based service and returns data as XML.
  • It only supports the HTTP protocol.
  • It is not open source but can be used by any client that understands XML.
  • It requires a SOAP protocol to receive and send data over the network, so it is not a light-weight architecture.

Web API:

  • A Web API is an HTTP based service and returns JSON or XML data by default.
  • It supports the HTTP protocol.
  • It can be hosted within an application or IIS.
  • It is open source and it can be used by any client that understands JSON or XML.
  • It has light-weight architecture and good for devices which have limited bandwidth, like mobile devices.

5. Should the Web API be seen as a replacement to WCF?

No, we cannot consider Web API as replacement of WCF because WCF is used for developing SOAP-based services whereas Web API is used for both SOAP-based and RESTful services.

While WCF offers request-reply, one-way, or duplex while Web API is by default request-reply only.

6. What is the minimum version of the .NET framework that could be expected to support Web API?

.NET 4.0 and above supports Web API

7. Which are all the main return types that the Web API can provide support?

A Web API controller action can return any of the following:

  • void - Return empty 204 (No Content)
  • HttpResponseMessage - Return empty 204 (No Content)
  • IHttpActionResult - Call ExecuteAsync to create an HttpResponseMessage, then convert to an HTTP response message
  • Some other type - Write the serialized return value into the response body; return 200 (OK).

8. Which JSON library is used for serialization?

JSON.NET

9. What is Attribute Routing in ASP.NET Web API?

Routing is how Web API matches a URI to an action. Web API 2 supports a new type of routing, called attribute routing. As the name implies, attribute routing uses attributes to define routes. Attribute routing gives you more control over the URIs in your web API.

Example

[Route("customers/{customerId}/orders")] //attribute routing
public IEnumerable<Order> GetOrdersByCustomer(int customerId) { 
//do something

 }

10. How many methods are supported in the web API?

The four main HTTP methods (GET, PUT, POST, and DELETE) can be mapped to CRUD operations as follows:

  • GET retrieves the representation of the resource at a specified URI. GET should have no side effects on the server.
  • PUT updates a resource at a specified URI. PUT can also be used to create a new resource at a specified URI if the server allows clients to specify new URIs.
  • POST creates a new resource. The server assigns the URI for the new object and returns this URI as part of the response message.
  • DELETE deletes a resource at a specified URI.

11. Can I return a view from the web API Asp.Net Core?

No, you cannot return View, but you can return it as ContentResult, as below

[HttpGet]
public ContentResult Index() 
{
    return new ContentResult 
    {
        ContentType = "text/html",
        Content = "<div>Hello World</div>"
    };
}

12. What is CORS in API?

Cross-origin resource sharing (CORS) is a browser mechanism which enables controlled access to resources located outside of a given domain. It extends and adds flexibility to the same-origin policy (SOP).

However, it also provides potential for cross-domain attacks, if a website's CORS policy is poorly configured and implemented.

13. How do I enable CORS in C# web-api 2?

Here are the steps to enable CORS in web api

  • "Install-Package Microsoft.AspNet.WebApi.Cors" from NuGet Package Manager console
  • Once the above Package is installed, open the file App_Start/WebApiConfig.cs. Add the following code to the WebApiConfig.Register method
     public static void Register(HttpConfiguration config)
            {
                //add to enable cors
                config.EnableCors(cors);
               
    
                //old code, route config etc.
            }?
  • Once done, you can add "[EnableCORS]" attribute above Web-API Controller or Method, for example
    using System.Web.Http.Cors; //add namespace
    
    namespace WebService.Controllers
    {
        [EnableCors(origins: "http://example.com", headers: "*", methods: "*")]
        public class SampleController : ApiController
        {
            // your API Controller code here
        }
    }?

Source: How do I enable CORS in C# web-api 2?

14. What's the difference between OpenID and OAuth

OpenID is about authentication (ie. proving who you are), OAuth is about authorisation (ie. to grant access to functionality/data/etc..without having to deal with the original authentication).

OAuth could be used in external partner sites to allow access to protected data without them having to re-authenticate a user.

OAuth was created to remove the need for users to share their passwords with third-party applications.

OpenID's most important feature is its discovery process. OpenID does not require hard coding each the providers you want to use ahead of time. Using discovery, the user can choose any third-party provider they want to authenticate.

15. What is OWIN in Web API?

Open Web Interface for . NET (OWIN) defines an abstraction between . NET web servers and web applications.

OWIN decouples the web application from the server, which makes OWIN ideal for self-hosting a web application in your own process, outside of IIS.

You may also like to read:

Linq interview questions and answers

Typescript Interview questions with Answers

15+ WCF Interview questions and answers

ASP.NET Core Interview questions and answers

Reverse string program in C# (Various methods)

Armstrong number in C# (Console Application Program)