Unit 3
Unit 3
• 7 Feb 2000: Web API was first introduced in the wild along with the introduction of
Salesforce on 7th Feb. The Salesforce company officially launched its API at IDG Demo
2000 conference.
• 20 Nov 2000: eBay launched their eBay API on 20 November 2000 with eBay Developers
Program.
• 16 July 2002: Amazon launched Amzon.com web services on 16 July 2002 that allows
developers to implement content and features of amazon.com into their own websites.
Web API was first started in early e-commerce on the Internet. At present, web APIs are
still recognized as a hobby by mainstream businesses.
• It is used to provide an interface for websites and client applications to have access to
data.
• It can also be used to access data from the database and save data back to the
database.
• It supports different text formats such as XML, JSON, etc.
• It is suitable or compatible with any type of browser and any type of device like
mobile, desktop, web, etc.
• It uses low bandwidth such as XML or JSON data, etc., and is therefore considered
good for devices that have limited bandwidth such as smartphones, etc.
• From a business point of view, web API is more applicable for UI/UX, increases interest
in the company’s product and services, increases website traffic.
What is Web API and why we use it ?
Web API (Application Programming Interface), as the name suggests, is an API that can
be accessed over the Web using the 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.
It does not have any specific data type. It can return data of any type depending upon
the business requirement. There are many HTTP methods like GET, POST, PUT, etc.,
which can return data in different formats depending upon the use case.
Web API: It 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 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: It 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: It is simply referred to as web services executing such as architecture.
REST is very important and beneficial in Web API because of the following reasons:
Filters are basically used to add extra logic at different levels of Web API framework
request processing. Different types of Web API filters are available as given below:
MVC (Model, View, and Controller) is basically an application design model 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. It is
mostly used for developing model user interfaces. 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.
ApiController: It is used to return data that is arranged in series and then sent to the
client.
Routing is the most important part of ASP.NET Web API. Routing is a way how Web
API matches a URI to an action. It is basically a process that decides which action and
controller should be called. The controller is basically a class that handles all HTTP
requests. All public methods of controllers are basically known as action methods or
just actions. Whenever a Web API framework receives any type of request, it routes
that request to action.
There are basically two ways to implement routing in Web API as given below:
Convention-based routing: Web API supports convention-based routing. In this type
of routing, Web API uses route templates to select which controller and action method
to execute.
Attribute-based routing: Web API 2 generally supports a new type of routing known
as attribute routing. As the name suggests, it uses attributes to define routes. It is the
ability to add routes to the route table via attributes.
Explain MVC flow diagram.
Flow Steps
Step 1 − The client browser sends request to the MVC Application.
Step 2 − Global.ascx receives this request and performs routing based on the URL of
the incoming request using the RouteTable, RouteData, UrlRoutingModule and
MvcRouteHandler objects.
Step 3 − This routing operation calls the appropriate controller and executes it using the
IControllerFactory object and MvcHandler object's Execute method.
Step 4 − The Controller processes the data using Model and invokes the appropriate
method using ControllerActionInvoker object
Step 5 − The processed Model is then passed to the View, which in turn renders the
final output.
MVC Components
Following are the components of MVC −
Model
The Model component corresponds to all the data-related logic that the user works with.
This can represent either the data that is being transferred between the View and
Controller components or any other business logic-related data. For example, a
Customer object will retrieve the customer information from the database, manipulate it
and update it data back to the database or use it to render data.
View
The View component is used for all the UI logic of the application. For example, the
Customer view will include all the UI components such as text boxes, dropdowns, etc.
that the final user interacts with.
Controller
Controllers act as an interface between Model and View components to process all the
business logic and incoming requests, manipulate data using the Model component and
interact with the Views to render the final output. For example, the Customer controller
will handle all the interactions and inputs from the Customer View and update the
database using the Customer Model. The same controller will be used to view the
Customer data.