Thursday, June 12, 2014

Simple Example of Web API in Asp.net

I am creating a very simple example which displays list of customer.

Steps:

1. Create Asp.net Empty Web Application.

2. Create Customer class like this

Public Class Customer

    Public Property Name As String
    Public Property City As String

End Class
3. Adding a Controller.


In Web API, a controller is an object that handles HTTP requests.

Create CustomersController class (Add "Web API Controller Class" and remvoe extra stuff) like this.
Imports System.Net
Imports System.Web.Http
 
Public Class CustomersController
    Inherits ApiController
     
    ' GET api/<controller> '
    Public Function GetAllCustomers() As IEnumerable(Of Customer)
        'Creating test data '
        Dim customers As New List(Of Customer)
        Dim objCustomer As New Customer
        objCustomer.Name = "Miller"
        objCustomer.City = "Camp Hill"
        customers.Add(objCustomer)
        objCustomer = New Customer
        objCustomer.Name = "John"
        objCustomer.City = "Harrisburg"
        customers.Add(objCustomer)
        objCustomer = New Customer
        objCustomer.Name = "Wayne"
        objCustomer.City = "Enola"
        customers.Add(objCustomer)
        objCustomer = New Customer
        objCustomer.Name = "Julie"
        objCustomer.City = "Enola"
        customers.Add(objCustomer)

        Return customers
    End Function
     
End Class
4. Calling the Web API with Javascript and jQuery

In this section, we'll add an HTML page that uses AJAX to call the web API. We'll use jQuery to make the AJAX calls and also to update the page with the results.

Create HTML page (index.html) like this
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <div>
        <h2>All Customers</h2>
        <ul id="customers" ></ul>
    </div>
    <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
    <script>
        var uri = 'api/customers';

        $(document).ready(function () {
            // Send an AJAX request
            $.getJSON(uri)
                .done(function (data) {
                    // On success, 'data' contains a list of products.
                    $.each(data, function (key, item) {
                    // Add a list item for the product.
                    $('<li>', { text: formatItem(item) }).appendTo($('#customers'));
                    });
                });
        });

        function formatItem(item) {
            return item.Name + ': ' + item.City;
        }
         
    </script>

</body>
</html>
5. Create Global.asax page like this
Imports System.Web.SessionState
Imports System.Web.Http

Public Class Global_asax
    Inherits System.Web.HttpApplication

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
 
        Dim route As New Routing.HttpRoute("api/{controller}")

        GlobalConfiguration.Configuration.Routes.Add("DefaultApi", route)
         
    End Sub
     

End Class
That's it! When you run your web application. Result will be:

All Customers

  • Miller: Camp Hill
  • John: Harrisburg
  • Wayne: Enola
  • Julie: Enola

Project Structure:

No comments:

Post a Comment

React-select is very slow on larger list - Found solution - using react-window

 I had more than 4000 items in searchable dropdownlist. I have used react-select but it was very slow. finally I found complete solution to ...