Basics of ASP NET Web API


What is ASP.NET Web API?

ASP.NET Web API is a framework for building Web API’s (Application Programming Interface), i.e. HTTP based services on top of the .NET Framework. 

The most common use of Web API is for building RESTful services. These services can then be consumed by a broad range of clients like,
1. Browsers
2. Mobile applications
3. Desktop applications
4. IOTs

 

What are RESTful services?

REST stands for Representational State Transfer. REST is an architectural pattern for creating an API that uses HTTP as its underlying communication method. 

REST was first introduced in the year 2000 by Roy Fielding as part of his doctoral dissertation. 


The REST architectural pattern specifies following set of constraints that a system should adhere to.

  • Client Server constraint - Client sends a request and the server sends a response. This separation of concerns supports the independent evolution of the client-side logic and server-side logic.
  • Stateless constraint - The communication between the client and the server must be stateless between requests. Means we should not be storing anything on the server related to the client. The request from the client should contain all the necessary information for the server to process that request. This ensures that each request can be treated independently by the server.
  • Cacheable constraint - Some data provided by the server does not change that often. This constraint says that let the client know how long this data is good for, so that the client does not have to come back to the server for that data over and over again. This will avoid unnecessary processing and significantly improve performance.
  • Uniform Interface - The uniform interface constraint defines the interface between the client and the server. In the context of a REST API, resources typically represent data entities. Product, Employee, Customer etc are all resources. The HTTP verb (GET, PUT, POST, DELETE) that is sent with each request tells the API what to do with the resource. Each resource is identified by a specific URI (Uniform Resource Identifier). 
    • HATEOAS stands for Hypermedia as the Engine of Application State. All this means is that in each request there will be set of hyperlinks that lets you know what other actions can be performed on the resource. 

 

Difference between WCF and Web API?

WCF (Windows Communication Foundation) can be used for creating RESTful services is WCF. The problem with WCF is that, a lot of configuration is required to turn a WCF service into a RESTful service. The more natural choice for creating RESTful services is ASP.NET Web API, which is specifically created for this purpose.

 

Web API

WCF

Supports only HTTP protocol

Supports HTTP, TCP, UDP & Custom transport   protocol

Uses routing and controller concepts similar to ASP.NET MVC

Uses Service, Operations & Data Contracts

Does not supports reliable messaging & transactions.
Suitable for building RESTful HTTP based service

Supports reliable messaging and transactions.
Can build service with WS-* standards like Reliable Messaging, Transactions, Message Security.

Configuration set using HTTPConfiguration Class

Uses web.config & attributes to configure a service

Ideal for building RESTful services

Supports RESTtful services but with some limitation

More suitable if you are targeting the broad range of device like PC, Mobile, Tablet and smart TV etc

Suitable for Request-Reply, One Way, and Duplex message exchange patterns.


Comments