Technically, A Web Application Consists of Two Types of Scripts

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

What is Web API?

o Web API is the enhanced form of the web application to provide services on different devices like laptop,
mobile, and others.
o Today, all kind of businesses use the internet as a cost-effective way to expand their business in the
international market.
o Web application helps to exchange information on the internet and also helps to perform a secure
transaction on web sites.
o Web applications are popular as the web browser is available in default, we don't need any installation of
software on computers with operating systems.
o For example, Facebook (a social networking web application), Flickr (a photo-sharing web application), and
Wikipedia are majorly used example of a web application.

 Technically, a web application consists of two types of


scripts:
1) Client-side scripts: JavaScript, HTML, and other client-side scripting languages are
used to design the web forms to present information to users.

2) Server-side scripts: ASP and other server-side scripting languages are used to


perform business logic and database related operations like storing and
retrieving information.

Who uses API?


These services can be accessed by different kind of users like:

o Web Browsers
o Mobile applications
o Desktop applications
o IOTs (Internet of Things)

ASP.NET Web API


API stands for the Application Programming Interface.

For example, we make a reservation from different web applications like MakeMyTrip, Ixigo or Paytm and all other
reservation web applications, but all applications make a reservation using credentials from IRCTC web site only, i.e.,
user performing reservation must have login credentials of IRCTC web site.
These services can be accessed by different kind of users like:

o Web Browsers
o Mobile applications
o Desktop applications
o IOTs (Internet of Things)

Web API services are used when the application is to be used on a distributed system.

Web API takes requests from the different type of client devices like mobile, laptop, etc. and sends them to the web-
server to process it and returns the desired data to the client.

Custom Web Application using a different kind of API's

Web API is System-System interaction, where information from one system is processed by another system, and
resultant data is shown to the viewer.

Example from Web API examples, we want McDonald's burger.

Let us suppose McDonald's only gives permission for takeaways to cooks only and not for others. Here McDonalds-
Takeaways (cook) is like an API, which allows other systems (cooks) to access the services and provide desired data.
ASP.NET Web API features
1) ASP.NET Web API is much similar to ASP.NET MVC.

2) It contains similar features as ASP.NET MVC like:

o Routing
o Controllers
o Action results
o Filter
o Model, etc.

There is a misconception that ASP.NET Web API is a part of ASP.NET MVC framework, while it can be used with any
other type of web application.

4)Stand-alone services can be developed using the Web API.

5) ASP.NET Web API framework is widely used to develop RESTful services.

RESTful services
o Web API is the enhanced form of a web application.
o SOAP (Simple Object Access Protocol) was an XML based protocol for developing the connected web
applications.
o Problem with the SOAP was that with each request, Metadata is attached with data to be transferred.
o This Metadata converts small data to heavy data on the server.
o Web API may or may not be RESTful services, but they are always HTTP based services.
o REST stands for Representational State Transfer.
o In REST API, only the state of the object is sent to the server to find the desired result.
o REST is an architectural pattern for developing an API that uses HTTP as its underlying communication
method.

When we are using HTTP based service, for example, BookMyShow app, we need data in managed form like JSON
format, XML format.

Client requests for the information by sending parameters using API methods.


For example, if we want to book a show for which we want to know the details like City, Movie Name, Place, Timing.
We will send the state of the object to the web-server, and API will check whether the data is available or not.

If the data is available (the movie is available for that instance), then it will send back the response to the client with
the object.

Values of an object are sent to the client, i.e., basically state of an object is sent to the client, so each time you don't
have to create an object.

It is a stateless, client-server model.


Methods of REST API
o Working on web technologies, we work on CRUD applications.
o In these applications, CRUD is to CREATE, READ, UPDATE, and DELETE a resource.
o Here, a resource is defined as the desired result. For example: https://in.bookmyshow.com/noida/movies
o In the above example, the user is looking for movies in city Noida on bookmyshow application, so the
resource is the data, the client is looking for.
o To perform these actions like to create a resource, read a resource, update a resource, or delete a resource,
we can use HTTP methods also called as REST methods.
o Now the basic CRUD operations are mapped to the HTTP protocols in the following manner:

CRUD Methods REST API Methods Description

Create POST Method refers to the C(Create) part of the CRUD.


It is used to create a resource.

Read GET Method refers to the R(Retrieve) part of the CRUD.


It is used to read a resource.

Update PUT Method refers to the U(Update) part of the CRUD.


It is used to update a resource.

Delete DELETE Method refers to the D(Delete) part of the CRUD.


It is used to delete a resource.
o REST API is best used for distributed systems.
o A distributed database is used everywhere, database stores data, on which CRUD (Create, Retrieve, Update,
and Delete) operations are performed.

So let's have an example of getting some dishes from the resource:

www.testwebsite.com/dishes

Now let's assume we want some specific dish from resource:

www.testwebsite.com/dishes/2

Implementing REST API Simple application


Let's create a REST API service for students.

o Firstly open Visual Studio (here we have Visual Studio 2019).


o Now select Create a new project:
o Now select the template ASP.NET Web Application (.NET Framework) with C# and name the project as
"WebApiStudentsSample".

o In the ASP.NET Project dialog, select the Empty template and also check Web API option. Click OK.
o A default structure generated will be as follows:

Adding Student.cs class to the Models folder

o Add a class with the name "Student" to define properties and other business logic. We can also define other
logics as validation, data access, etc.
o Right-click on Models, Select Add option, and then select Class and give a name to the class as "Student.cs"
Add the following code to define properties for student class:

namespace WebApiStudentsSample.Models  
{  
    public class Students  
    {  
        public int StudentId  
        {  
            get;  
            set;  
        }  
        public string StudentName  
        {  
            get;  
            set;  
        }  
        public string Address  
        {  
            get;  
            set;  
        }  
        public string Course  
        {  
            get;  
            set;  
        }  
  
    }  
}  

Adding StudentControllers.cs class to Controllers folder

o Controller class handles HTTP request from the client, which may be a desktop application, mobile device,
and browser.
o Right-click on Controllers, Select Add option, and then select 'Controller..'.

Add Scaffold window will open then Select Web API2 Controller ?Empty template.


o After selecting Scaffolding template, Add controller window will open and give a name to the class as
"StudentController".

o Scaffolding creates a "StudentController.cs" class inside the controller folder.


Now implement methods to perform CRUD operations. Let us assume we want all student details and also details for
some specific student.

Now add following code:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Net;  
using System.Net.Http;  
using System.Web.Http;  
using WebApiStudentsSample.Models;  
  
namespace WebApiStudentsSample.Controllers  
{  
    public class StudentController : ApiController  
    {  
        IList<Students> Students = new List<Students>()  
        {  
            new Students()  
                {  
                    StudentId = 1, StudentName = "Mukesh Kumar", Address = "New Delhi", Course = "IT"  
                },  
                new Students()  
                {  
                    StudentId = 2, StudentName = "Banky Chamber", Address = "London", Course = "HR"  
                },  
                new Students()  
                {  
                    StudentId = 3, StudentName = "Rahul Rathor", Address = "Laxmi Nagar", Course = "IT"  
                },  
                new Students()  
                {  
                    StudentId = 4, StudentName = "YaduVeer Singh", Address = "Goa", Course = "Sales"  
                },  
                new Students()  
                {  
                    StudentId = 5, StudentName = "Manish Sharma", Address = "New Delhi", Course = "HR"  
                },  
        };  
        public IList<Students> GetAllStudents()  
        {  
            //Return list of all employees    
            return Students;  
        }  
        public Students GetStudentDetails(int id)  
        {  
            //Return a single employee detail    
            var Student = Students.FirstOrDefault(e => e.StudentId == id);  
            if (Student == null)  
            {  
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));  
            }  
            return Student;  
        }  
    }  
}  

In the StudentController class controller, you can see that the method "GetAllStudents" return the list of all
students and the method "GetStudentDetails" returns the detail of single student.

In the following table, you can understand how controller use route URL to perform CRUD action.

Controller Method Route URI(Uniform Resource Identifier)

GetAllStudents /api/ student

GetStudentDetails /api/ student /id

Run The Web API

To run a Web API, firstly press F5 or Ctrl+F5 or Click on IIS express run icon then the browser will open with the URL
like https://localhost:44329/

Now to find the list of all students edit the URL as https://localhost:44329/api/student
To fetch the details of a single student, Edit the URL as:

https://localhost:44329/api/student/2
Need for Web API
o A Web API helps to access service data from different internet devices like browsers, mobile apps, and other
devices.
o Helps to work on RESTful web services.
o Helps to develop light weighted and maintainable Web Services.
o Used to create both types of services RESTful and non-RESTful services.
o Also supports JSON, XML, and other data formats.
o Helps to develop services supporting all features of HTTP services such as like  caching, request/response
headers, versioning, etc.

You might also like