ASP.NET Web API Tutorial
In this tutorial, we will discuss the following topics:
ASP.NET Web API tutorial provides basic and advanced concepts of ASP.NET. Our ASP.NET tutorial is designed for beginners and professionals both.
What is Web API?
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:
ASP.NET Web API
API stands for the Application Programming Interface.
"ASP.NET Web API is an extensible framework for building HTTP (Hypertext Transfer Protocol) services that can be accessed from any client such as browsers and mobile devices."
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:
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.
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.
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:
3) 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.
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.
"REST API is an architectural style as well as an approach for communications purpose that is often used in various web developments."
It is a stateless, client-server model.
Principles of REST API
The six principles of REST API are:
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).
2) Client-Server: Separating the functionality helps to increase user interface portability across multiple platforms as well as extended the scalability of the server components.
3) Uniform Interface: To obtain the uniformity throughout the application, REST has defined four interface constraints for which are:
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.
5) Layered System: The layered system allows an application to be most stable by limiting component behavior. The layered architecture also helps to enhance security as the component at each layer cannot interact beyond each layer they are in.
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.
Methods of REST API
Now the basic CRUD operations are mapped to the HTTP protocols in the following manner:
So let's have an example of getting some dishes from the resource:
Now let's assume we want some specific dish from resource:
Implementing REST API Simple application
Let's create a REST API service for students.
Adding Student.cs class to the Models folder
Add the following code to define properties for student class:
Adding StudentControllers.cs class to Controllers folder
Add Scaffold window will open then Select Web API2 Controller ?Empty template.
Note: We can see that the controller class inherits the System.Web.Http.ApiController class instead of the Controller class.
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:
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.
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:
Need for Web API
MVC vs. Web API
Differences between ASP.NET API and ASP.NET MVC are as follows:
Web API vs. WCF
Differences between ASP.NET WCF and ASP.NET Web API are as follows:
Web API vs. Web Services
Differences between Web API and Web services are as follows:
Web API Security
ASP.NET Web API Authentication and Authorization are important concepts for Web API security.
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.
If a request requires authentication in web API, but the client does not provide the credentials in the request header, then the server will return 401 (Unauthorized).
Before learning Web API, the user must have basic knowledge of Http methods, C# properties also Knowledge of MVC will be plus point.
This tutorial is for beginners and professionals to learn Web API and RESTful services.
This tutorial is available in a simple and easy language still, if there is any confusion, kindly post the doubt in the contact form.