Technically, A Web Application Consists of Two Types of Scripts
Technically, A Web Application Consists of Two Types of Scripts
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.
o Web Browsers
o Mobile applications
o Desktop applications
o IOTs (Internet of Things)
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.
Web API is System-System interaction, where information from one system is processed by another system, and
resultant data is shown to the viewer.
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.
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.
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.
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.
www.testwebsite.com/dishes
www.testwebsite.com/dishes/2
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:
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;
}
}
}
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..'.
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.
GetAllStudents /api/ student
GetStudentDetails /api/ student /id
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.