ASP WEB API 2
ASP WEB API 2
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.
o Web Browsers
o Mobile applications
o Desktop applications
o IOTs (Internet of Things)
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.
Let's take another 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.
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.
1. Stateless
2. Client-Server
3. Uniform Interface
4. Cacheable
5. Layered System
6. Code on demand
1) Stateless: When the request from the client is sent to the server, it contains
all the required information to make the server process it. A request may be
part of QueryString or URL.
For example, let's suppose the resource is requested from the URL (Uniform
Resource Locator).
o Resource Identification
o Resource Manipulation using representations
o Self-descriptive massages
o And hypermedia as the engine of the web application
4) Cacheable: In order to provide a better performance, applications are made
cacheable. It is done by marking the response as cacheable or non-cacheable
implicitly or explicitly. If the resource is defined cacheable, then the client cache
can reuse response data for equivalence requests.
6) Code on demand: Constraint that is used optionally and least. Analyze and
simplify the client by creating a smart application which doesn't lie on its own
code structure.
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"
1. namespace WebApiStudentsSample.Models
2. {
3. public class Students
4. {
5. public int StudentId
6. {
7. get;
8. set;
9. }
10. public string StudentName
11. {
12. get;
13. set;
14. }
15. public string Address
16. {
17. get;
18. set;
19. }
20. public string Course
21. {
22. get;
23. set;
24. }
25.
26. }
27. }
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".
1. using System;
2. using System.Collections.Generic;
3. using System.Linq;
4. using System.Net;
5. using System.Net.Http;
6. using System.Web.Http;
7. using WebApiStudentsSample.Models;
8.
9. namespace WebApiStudentsSample.Controllers
10. {
11. public class StudentController : ApiController
12. {
13. IList<Students> Students = new List<Students>()
14. {
15. new Students()
16. {
17. StudentId = 1, StudentName = "Mukesh Kumar", Ad
dress = "New Delhi", Course = "IT"
18. },
19. new Students()
20. {
21. StudentId = 2, StudentName = "Banky Chamber", A
ddress = "London", Course = "HR"
22. },
23. new Students()
24. {
25. StudentId = 3, StudentName = "Rahul Rathor", Addr
ess = "Laxmi Nagar", Course = "IT"
26. },
27. new Students()
28. {
29. StudentId = 4, StudentName = "YaduVeer Singh", A
ddress = "Goa", Course = "Sales"
30. },
31. new Students()
32. {
33. StudentId = 5, StudentName = "Manish Sharma", Ad
dress = "New Delhi", Course = "HR"
34. },
35. };
36. public IList<Students> GetAllStudents()
37. {
38. //Return list of all employees
39. return Students;
40. }
41. public Students GetStudentDetails(int id)
42. {
43. //Return a single employee detail
44. var Student = Students.FirstOrDefault(e => e.StudentId
== id);
45. if (Student == null)
46. {
47. throw new HttpResponseException(Request.CreateRe
sponse(HttpStatusCode.NotFound));
48. }
49. return Student;
50. }
51. }
52. }
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.
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/
https://localhost:44329/api/student/2
Represents resultant data in JSON format using Return data in particular format like J
JsonResult. any other format
ServiceContract a
defines a Service Co
contains operations
OperationContrac
Uses HTTP verbs as methods defines the operatio
Use of model
called as CRUD operations be used.
DataContract attri
the properties or typ
be transferred betw
parties.
Suppose a web API is created, and the access to the API is for some specific
users, and also different operations are available for different users.
Prerequisite
Before learning Web API, the user must have basic knowledge of Http methods,
C# properties also Knowledge of MVC will be plus point.
Audience
This tutorial is for beginners and professionals to learn Web API and RESTful
services.
Problems
This tutorial is available in a simple and easy language still, if there is any
confusion, kindly post the doubt in the contact form.
https://www.javatpoint.com/web-api