Introduction
In this article, we will see some important things in the Web API. It provides the output in XML or JSON format and we can also search records by ID. This article describes the scalable architecture for building the services that build on the HTTP. Web API is the extensible framework for creating HTTP services in ASP.NET.
Use the following procedure to start the application.
Step 1
Create application using the following:
- Start Visual Studio 2013.
- From Start Window Select "New Project".
- Select "Installed" -> "Template" -> "Visual C#" -> "Web" -> "Visual Studio 2012" and then select "ASP.NET MVC4 Web Application".
![Select MVC4]()
- Click on the "OK" button.
- From the MVC4 project window select "Web API".
![Select Web API]()
- Click on the "OK" button.
Step 2
Now we add the model class to the application; we can say that it is the data in our application. We pass the HTTP message to the server then the HTTP server serializes the request to the model to JSON or XML.
- In the Solution Explorer.
- Right-click on the Model Folder.
- Select "Add" -> "Class".
- Select "Installed" -> "Visual C#"-> and select class.
![Add Model class]()
- Click on the "OK" button.
Add the following code:
Step 3
Use the following procedure to add the Controller.
- In the "Solution Explorer".
- Right-click on the "Controller folder".
- Select "Add" -> "Controller".
- Select "API Controller" from the template.
![Add API Controller]()
- Click on the "Add" button.
Add the following code:
All the data is stored in an array and we have declared two methods in the controller class, one is GetPlayers() that fetches all the player's names from the controller. And another is GetCricketerById(int Id) that fetches a single cricketer by the ID.
Step 4
Now we will create a small JavaScript client application, a JavaScript client that consumes the client order. And we will modify it in the index.cshtml code.
![index page]()
Add the following code:
Find the record by ID.
![find record by ID]()
Step 5
Now we will execute the application using Fiddler. It displays the record in JSON format.
![JSON format Result]()
We can also see the results in XML format.
![XML Output]()