C#

Web api interview questions and answers


Q.  What is Web API ?

Ans. WebAPI is a framework which helps you to build/develop HTTP services.

Q.  Why is Web API required? Is it possible to use RESTful services using WCF?
Ans. Yes, we can still develop RESTful services with WCF. However, there are two main reasons that prompt users to use Web API instead of RESTful services.
  Web API increases TDD (Test Data Driven) approach in the development of RESTful services.
  If we want to develop RESTful services in WCF, you surely need a lot of config settings, URI templates, contracts & endpoints for developing RESTful services using web API.

Q.  Why select Web API?
Ans.
  It is used to create simple, non-SOAP-based HTTP Services
  It is also an easy method for creation with Web API. With WCF REST Services
  It is based on HTTP and easy to define, expose and consume in a REST-ful way.
  It is lightweight architecture and ideal for devices that have limited bandwidth like smart phones.

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

Ans. It’s a not at all true that ASP.NET Web API has replaced WCF. In fact, it is another way of building non-SOAP based services, i.e., plain XML or JSON string.

Q.  What are the advantages of Web API?
Ans. Advantages of Web API are:
   OData
   Filters
   Content Negotiation
   Self-Hosting
   Routing
   Model Bindings

Q.  What are main return types supported in Web API?

Ans. A Web API controller action can return following values:
   Void – It will return empty content
          HttpResponseMessage – It will convert the response to an HTTP message.
          IHttpActionResult – internally calls ExecuteAsync to create an HttpResponseMessage
          Other types – You can write the serialized return value into the response body
Q.  Web API supports which protocol?
Ans. HTTP protocol.
Q.  Which .NET framework supports Web API?
Ans. NET 4.0 and above version supports web API.
Q.  Web API uses which of the following open-source library for JSON serialization?
Ans. Web API uses Json.NET library for JSON serialization.
Q.  By default, Web API sends HTTP response with which of the following status code for all uncaught exception?
Ans. 500 – Internal Server Error
Q.  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.
Q.  What is the benefit of using REST in Web API?
Ans. REST is used to make fewer data transfers between client and server which make it an ideal for using it in mobile apps. Web API also supports HTTP protocol. Therefore, it reintroduces the traditional way of the HTTP verbs for communication.
Q.  How can we use Web API with ASP.NET Web Form?
Ans. Web API can be used with ASP.NET Web Form
It can be performed in three simple steps:
1.         Create a Web API Controller,
2.         Add a routing table to Application_Start method of Global.sax
3.         Then you need to make a jQuery AJAX Call to Web API method and get data.
Q.  How to you can limit Access to Web API to Specific HTTP Verb?
Ans. Attribute programming plays a important role. It is easy to restrict access to an ASP.NET Web API method to be called using a particular HTTP method.
Q.  Can you use Web API with ASP.NET Web Form?
Ans. Yes, It is possible to use Web API with ASP.Net web form. As it is bundled with ASP.NET MVC framework. However, it can be used with ASP.NET Web Form.
Q.  Explain exception filters?
Ans. It will be executed when exceptions are unhandled and thrown from a controller method. The reason for the exception can be anything. Exception filters will implement “IExceptionFilter” interface.
Q.  How you can return View from ASP.NET Web API method?
Ans. No, we can’t return a view from ASP.NET Web API Method. Web API creates HTTP services that render raw data. However, it’s also possible in ASP.NET MVC application.
Q.  How to register exception filter globally?
Ans. It is possible to register exception filter globally using following code-
GlobalConfiguration.Configuration.Filters.Add(new MyTestCustomerStore.NotImplExceptionFilterAttribute());
Q.  Explain what is REST and RESTFUL?
Ans. REST represents Representational  State Transfer; it is entirely a new aspect of writing a web app.
RESTFUL: It is term written by applying REST architectural concepts is called RESTful services. It focuses on system resources and how the state of the resource should be transported over HTTP protocol.
Q. How can you handle errors in Web API?
Ans. Several classes are available in Web API to handle errors. They are HttpError, Exception Filters, HttpResponseException, and Registering Exception Filters.
Q. What New Features comes with ASP.NET Web API 2.0?
Ans. The latest features of ASP.NET Web API framework v2.0 are as follows:
          Attribute Routing
          Cross-Origin Resource Sharing
          External Authentication
          Open Web Interface NET
          HttpActionResult
          Web API OData
Q. How can you pass multiple complex types in Web API?
Ans. Two methods to pass the complex types in Web API –
Using ArrayList and Newtonsoft array
Q. Name the tools or API for developing or testing web api?
Ans. Testing tools for web services for REST APIs include:
1.         Jersey API
2.         CFX
3.         Axis
4.         Restlet
Q.  What is REST?
Ans. REST is architectural style. It has defined guidelines for creating services which are scalable. REST used with HTTP protocol using its verbs GET, PUT, POST and DELETE.
Q.  How to unit test Web API?
Ans. We can perform a Unit test using Web API tools like Fiddler.
Here, are some setting to be done if you are using
Fiddler –Compose Tab -> Enter Request Headers -> Enter the Request Body and execute
Q. How can we restrict access to methods with specific HTTP verbs in Web API?
Ans. Attribute programming is widely used for this functionality. Web API also allows restricting access of calling methods with the help of specific HTTP verbs. It is also possible to define HTTP verbs as attribute over method.
Q. Tell me the code snippet to show how we can return 404 errors from HttpError?
Ans. Code for returning 404 error from HttpError
string message = string.Format(“TestCustomer id = {0} not found”, customerid);
return Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
Q.  Web API supports which protocol?
Ans. Web App support HTTP protocol
Q.  Which of the following .NET framework supports Web API?
Ans. Web API is supported by NET 4.0 version
Q. Web API uses which library for JSON serialization?
Ans. Web API uses Json.NET library for JSON serialization.
Q.  How to handle errors in Web API?
Ans. Several classes are available in Web API to handle errors. They are HttpError, HttpResponseException, Exception Filters, Registering Exception Filters.
Q. What is the benefit of WebAPI over WCF?
Ans. WCF services use the SOAP protocol while HTTP never use SOAP protocol. That’s why WebAPI services are lightweight since SOAP is not used. It also reduces the data which is transferred to resume service. Moreover, it never needs too much configuration. Therefore, the client can interact with the service by using the HTTP verbs.
Q.  State differences between MVC and WebAPI
Ans. MVC framework is used for developing applications which have User Interface. For that, views can be used for building a user interface.
WebAPI is used for developing HTTP services. Other apps can also be called the WebAPI methods to fetch that data.
Q.  Who can consume WebAPI?
Ans. WebAPI can be consumed by any client which supports HTTP verbs such as GET, PUT, DELETE, POST. As WebAPI services don’t need any configuration, they are very easy to consume by any client. Infact, even portable devices like Mobile devices can easily consume WebAPI which is certainly the biggest advantages of this technology.


4 comments: