Introduction

In this article, I am going to explain how to create RESTful Service, using ASP.NET Web API.

What is REST?

REST stands for Representational State Transfer. It is a software design pattern, introduced as an alternative to SOAP-based data exchange, and is a way for programs to talk over the web.

What is Web API?

In its simplest form, a Web API is an API over the web (HTTP) and ASP.NET Web API is a framework that allows you to build Web APIs, i.e. HTTP-based services on top of the .NET Framework.

It contains only the following 4 methods to do the operations.

  • GET
  • POST – insert
  • PUT – update
  • DELETE

Steps to Create Application

  1. Create Web API Application.





  2. Add a class and its properties for creating in-memory object.




    1. namespace WebApiRestfulService.Models  
    2. {  
    3.     public class CustomerCore  
    4.     {  
    5.         public int Id { get; set; }  
    6.         public string Name { get; set; }  
    7.         public string Address { get; set; }          
    8.     }  
    9. }  
  3. Create Controller with API read/write action template. Note that here, we are adding API Controller so it's extending the ApiController.







  4. Create some new customer records using Controller constructor class.
    1. CustomerCore[] customers = new CustomerCore[]  
    2. {  
    3.     new CustomerCore{Id=1001, Name="Ramakrishna Basagalla", Address="Hyderabad"},  
    4.     new CustomerCore{Id=1002, Name="Praveenkumar", Address="Hyderabad"},  
    5.     new CustomerCore{Id=2001, Name="Rakesh", Address="Bangalore"},  
    6.     new CustomerCore{Id=2002, Name="Puneeth", Address="Bangalore"},  
    7.     new CustomerCore{Id=1003, Name="Prashanth", Address="Hyderabad"},  
    8.     new CustomerCore{Id=1004, Name="Shiva",Address="Hyderabad"}  
    9. };  
  5. Update the below code to get the customer values.
    1. // GET api/customer  
    2. public IEnumerable<CustomerCore> GetCustomers()  
    3. {  
    4.     return customers;  
    5. }  
    6.   
    7. // GET api/customer/5  
    8. public CustomerCore GetCustomerById(int id)  
    9. {  
    10.     var customer = customers.FirstOrDefault((c) => c.Id == id);  
    11.     if (customer == null)  
    12.     {  
    13.         throw new HttpResponseException(HttpStatusCode.NotFound);  
    14.     }  
    15.     return customer;  
    16. }  
  6. Execute the project. The Web API Service will return the values in form of JSON. We can see the values based on its id. It uses the second method to return these values.

    Note:
    If you browse the same URL using Chrome browser, you can see that in .xml format.





    http://localhost:64072/api/customer/1001






  7. Create an HTML page to access the service data.


    1. <table>  
    2.     <tr>  
    3.         <td>  
    4.             <div>  
    5.                 <h2>All Customers</h2>  
    6.                 <div id="myDiv"></div>  
    7.             </div>  
    8.         </td>  
    9.         <td>  
    10.             <div>  
    11.                 <h2>Search Customer By Id</h2>  
    12.                 <input type="text" id="txtCustId" />  
    13.                 <input type="button" id="btnSearch" value="Search" onclick="FindCustomer();" />  
    14.                 <p id="pCustomer"></p>  
    15.             </div>  
    16.         </td>  
    17.     </tr>  
    18. </table>  
  8. Here, on page load, I am retrieving the customer details using AJAX call.

    Here, we are passing the URL (api/customer/) to get the data. After success, we are generating it into list.
    1. <script type="text/javascript">  
    2. $(document).ready(function () {  
    3.     // Get all customers   
    4.     $.getJSON("api/customer/",  
    5.         function (data) {  
    6.             $.each(data, function (key, val) {  
    7.                 $('#myDiv').append("<ul id='ul" + val.Id + "'>" + val.Name + "</ul>");  
    8.                 $("#ul" + val.Id).append("<li>" + val.Id + "</li>");  
    9.                 $("#ul" + val.Id).append("<li>" + val.Address + "</li>");  
    10.   
    11.             });  
    12.         });  
    13. });  
    14.   
    15. // Get Customer by Id  
    16. function FindCustomer() {  
    17.     var id = $("#txtCustId").val();  
    18.     $.getJSON("api/customer/" + id,  
    19.         function (data) {  
    20.             $('#pCustomer').empty();  
    21.             $('#pCustomer').append("<ul id='pul" + data.Id + "'>" + data.Name + "</ul>");  
    22.             $("#pul" + data.Id).append("<li>" + data.Id + "</li>");  
    23.             $("#pul" + data.Id).append("<li>" + data.Address + "</li>");  
    24.         });  
    25. }  
    26. </script>  



Hope this article helps you in understanding the Web API concepts.