Introduction

In this article, I will create a read-only Web API. In this tutorial we will only implement the ReadOnly Method, that is a GET method.

Now let's see how to create the Web API application/

Step 1

Create an application using the following:

Step 2

Now add an API Controller to the project using the following:

Add the following code:

  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 ReadOnly.Models;
  8. namespace ReadOnly.Controllers
  9. {
  10. public class SimpleController : ApiController
  11. {
  12. public string[] Get()
  13. {
  14. return new string[]
  15. {
  16. "Hello",
  17. "India"
  18. };
  19. }
  20. }
  21. }

Step 3

Now execute the application in Internet Explorer.

Execute application in IE
Now to open the "Developer Tools" Press "F12" key from the keyboard. Now click on the "Network" tab, then the "Start Capturing" tab to begin capturing traffic.
For Start Capturing

Now we append the URL "http://localhost:5557/" with "http://localhost:5557/api/simple" and press Enter. The details of capturing is displayed in the network capture window. The MIME type is "application/JSON".

MIME type response

Now when we click on the "Go to detailed view" tab it displays all the details of the API. To see the JSON results click on the "Response Body" tab.

JSON Response

Step 4

Now we add a Model class to the Model folder.

Add the following code:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. namespace ReadOnly.Models
  6. {
  7. public class Simple
  8. {
  9. public int ID { get; set; }
  10. public string Name { get; set; }
  11. }
  12. }

Step 5

Now modify the GET method in the SimpleController class.

  1. public Simple[] Get()
  2. {
  3. return new Simple[]
  4. {
  5. new Simple
  6. {
  7. ID=1,
  8. Name="Smith Patel"
  9. },
  10. new Simple
  11. {
  12. ID=2,
  13. Name="Virat Kohli"
  14. },
  15. new Simple
  16. {
  17. ID=3,
  18. Name="Rohit Shrma"
  19. }
  20. };
  21. }

Now again execute the application to see the changes in the GET method of the Controller.

Step 6

We need to add a service folder to the project. Right-click on the project, select "Add New Folder" then change the name to "Services".

Add service folder in project

Step 7

Now add the Repository class to the service folder:

Add the following code in this repository class:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using ReadOnly.Models;
  6. namespace ReadOnly.Services
  7. {
  8. public class SimpleRepository
  9. {
  10. public Simple[] GetAllSimples()
  11. {
  12. return new Simple[]
  13. {
  14. new Simple
  15. {
  16. ID=1,
  17. Name="Smith Patel"
  18. },
  19. new Simple
  20. {
  21. ID=2,
  22. Name="Virat Kohli"
  23. },
  24. new Simple
  25. {
  26. ID=3,
  27. Name="Rohit Shrma"
  28. }
  29. };
  30. }
  31. }
  32. }

Step 8

In the SimpleController class add the private field for representing the instance of the repository class and change the code of the SimpleController class as in the following:

Add the following namespace in the controller:

  1. using ReadOnly.Services;

Now change the following code:

  1. private SimpleRepository simplerepository;
  2. public SimpleController()
  3. {
  4. this.simplerepository = new SimpleRepository();
  5. }
  6. public Simple[] Get()
  7. {
  8. return simplerepository.GetAllSimples();
  9. }

Step 9

Enter the breakpoint in the SimpleController class.

display value on break point

Again execute the application: