Web Api

Based on Web API Basic Interview Questions

Web API (Application Programming Interface), is an API that can be accessed through HTTP protocol. It is basically considered the best platform for revealing or uncovering data and services to various different services. It is a tool that can be used to push data to a server and can be accessed by server code. It can be built or developed using various technologies like java, ASP.NET, etc. Web API:

  • contains additional layers that simply standardize communications and provide different options on how to format input and output.
  • can be used with ASP.NET MVC and different types of web applications such as ASP.NET WebForms.
  • considered the best for creating resource-oriented services
  • helps to develop REST-ful services and SOAP-based services.

It does not have any specific data type. It can return data of any type depending on the business requirement. There are many HTTP methods like GET, POST, PUT, Patch, Delete, which can return data in different formats depending on the use case.

WCF is a framework used for developing SOAP (Service-oriented applications). This framework is used for developing, configuring, and deploying, or implementing network-distributed services.

Web API is an application programming interface for both web browsers and web servers. Browser API simply extends or increases the functionality of web browsers whereas Server API simply extends or increases the functionality of web server.

Web API
WCF
It is used to develop both SOAP-based services and RESTful services.
It is used to deploy only SOAP-based services.
It supports various MVC features such as routing, model binding, etc.
It does not support any MVC features.
It only supports HTTP protocol.
It supports various protocols such as HTTP, UDP, custom transport.
It is considered best for developing RESTFull services.
It supports only limited RESTFull services.
It is good for exposing extensive range of clients such as iPhones, browsers, mobile phones, tablets, etc.
It is good for creating services that use such transport channels as TCP, UDP, Named pipes, etc.
It offers support for UTF-8 encoding format.
It offers TEXT, Binary encoding support, MTOM (Message Transmission Optimization Mechanism), etc.

Web API is considered the best choice over WCF because of the following reasons:

  • Web API uses all features of HTTP such as URIs, request/response headers, caching, versioning, various content formats, etc.
  • One does not have to define or explain any extra config setting for different devices in Web API.
  • Web API uses different text formats including XML because of which it is faster and more preferred for lightweight services.
  • Web API also supports MVC features whereas WCF does not support MVC features.
  • Web API provides more flexibility as compared to WCF.
  • Web API uses standard security like token authentication, basic authentication, etc., to provide secure service whereas WCF uses WS-I standard to provide secure service.

REST (Representation State Transfer) API is basically an architectural style that makes productive use of existing technology and protocols of the web. It is a set of rules that developers need to follow when they develop their API or services that are scalable. It is used with HTTP protocol using its verbs such as GET, DELETE, POST, PUT.
RESTful API is simply referred to as web services executing such as architecture.

See also REST vs. RESTful: The Difference and Why the Difference Doesn’t Matter

REST is very important and beneficial in Web API because of the following reasons:

  • It allows less data transfer between client and server which is beneficial for mpbile apps .
  • It is easy to use and lightweight.
  • It provides more flexibility.
  • It also handles and controls various types of calls, returning various data formats.
  • It uses simple HTTP calls for inter-machine communication rather than using more complex options like CORBA, COM+, SOAP, or RPC.

REST (Representational State Transfer) is a new and improved form of web service. It describes the architectural style of networked systems. It does not require greater bandwidth when requests are sent to the server. It just includes JSON message. SOAP (Simple Object Access Protocol) is a simple and lightweight protocol that is generally used for exchanging structured and typed information on the Web. It works mostly with HTTP and RPC (Remote Procedure Call). This protocol is mainly used for B2B applications one can define a data contract with it. SOAP messages are heavier in content and therefore use greater bandwidth.

REST
SOAP
It is basically an architectural pattern.
It is basically a messaging protocol.
It usually works with various text formats such as plain text, HTML, JSON, XML, etc.
It works only with XML formats.
It is totally stateless.
It has some specifications for both stateless and stateful implementation.
Its performance is faster as compared to SOAP.
Its performance is slower as compared to REST.
It uses XML and JSON to send and receive data.
It uses WSDL (Web Service Description Language) for communication among consumers or users and providers.
REST has to resend transfer whenever it determines any errors.
SOAP includes built-in error handling for communications errors using WS-ReliableMessaging specification.
It calls services using the URL path.
It calls services by calling RPC (Remote Procedure Call) method.

It is basically an enhanced and modified feature of Web API. This new version supports such new features as:

  • New Routing Attribute
  • Secure ASP.NET Web API using OAuth 2.0
  • Support for Cross-Origin requests using CORS
  • IHttpActionResult return type
  • Support for $expand, $select in OData Service
Because of all the new features of Web API 2.0, it is considered an optimal choice and suitable development model that makes it easier to develop RESTful services interfaces to different clients running on various platforms. It also supports configuring routes in the Web API method or controller level.

In web API, media type formatters are classes that are responsible for serialization data. Because of serializing request/response data, Web API can understand request data format in a better way and send data in a format that the client expects. It simply specifies data that is being transferred among client and server in HTTP response or request.

Media Type Formatter Class
MIME Type
Description
JsonMediaTypeFormatter
application/json, text/json
Handles JSON format
XmlMediaTypeFormatter
application/xml, text/json
Handles XML format
FormUrlEncodedMediaTypeFormatter
application/x-www-form-urlencoded
Handles HTM form URL-encoded data
JQueryMvcFormUrlEncodedFormatter
application/x-www-form-urlencoded
Handles model-bound HTML form URL-encoded data

Web API generally supports only HTTP protocol.

Filters are basically used to add extra logic at different levels of Web API framework request processing.
Web Api includes the following types of filters:

  • Authentication Filter: It handles authentication and authenticates HTTP requests. It also helps to authenticate user detail. It checks the identity of the user.
  • Authorization Filter: It handles authorization. It runs before controller action. This filter is used to check whether or not a user is authenticated. If the user is not authenticated, then it returns an HTTP status code 401 without invoking the action.
    Web API has built-in authorization filter in the form of AuthorizeAttribute
  • Action Filter: It is used to add extra logic before or after controller action executes. It is simply a way to add extra functionality to Web API services. Action Filter can be applied to controller action or entire controller.
  • Exception Filter: It is used to handle exceptions that are unhandled in Web API. It is used whenever controller actions throw an unhandled exception that is not HttpResponseException. It implements an IExceptionFilter interface.
  • Override Filter: It is used to exclude specific action methods or controllers from the global filter or controller level filter. It is simply used to modify the behavior of other filters for individual action methods.

Web API provides flexibility in terms of handling errors. Exception handling is a technique that is used to handle run-time errors in application code. We can use HttpResponseException, HttpError, Exception filters, register exception filters, Exception handlers to handle errors. Exception filter can be used to identify unhandled exceptions on actions or controllers. Exception handlers can be used to identify any type of unhandled exception application-wide, and HttpResponseException can be used when there is the possibility of an exception.

One can register exception filter globally using following code:
GlobalConfiguration.Configuration.Filters.Add ( new MyTestCustomerStore.NotImplExceptionFilterAttribute());

MVC (Model, View, and Controller) is basically an application design pattern that comprises three interconnect parts i.e., model, view, and controller. It allows coders to factor out different components of the application and update them more easily. Its main purpose is to display patterns in structure for keeping display and data separate to enable both of them to change without affecting others.

MVC
Web API
It is used to build Web applications that reply as both data and views.
It is used to build HTTP services that reply only as data.
It returns data in JSON format by using JSONResult.
It returns data in different formats such as JSON, XML, etc.
It supports content negotiation, self-hosting.
It does not support content negotiation, self-hosting.
It cannot be used to build REST-full services.
It is used in creating RESTfull services.
It returns a view (HTML)
It returns REST responses.

ASP.NET Web API is a framework that is used for developing HTTP services to provide responses to client requests. It can be accessed in different applications on different platforms. It is provided for developing and consuming HTTP-based services on top of .NET Framework. These services can be used by such clients as:

  • Desktop Applications
  • Mobile Applications
  • I(nternet)O(f)T(hings)s
  • Browsers

Usage of ASP.NET Web API provides the following advantages:

  • It provides convenient platform for developing RESTful applications on .NET Framework.
  • It works the same way that HTTP works with help of HTTP verbs such as GET, POST, PUT, DELETE for all crud operations.
  • It completely supports routing.
  • It supports model binding, validation, Odata (Open Data Protocol) that allows creation and consumption of RESTful APIs services.
  • It enables developing custom help and test pages with help of ApiExplorer.
  • One can develop non-SOAP-based services such as plain XML, JSON strings, etc.
  • It enables TDD (Test Driven Development) approach in the development of RESTful services.