Skip to main content

How to get/fetch data from ASP.Net WebAPI or how to query ASP.Net WebAPI.

In this article i am going to explain about How to get/fetch data from the WebAPI or how to query WebAPI.

In my previous article i have explained about How To Create Your First WEB API Project In ASP.Net MVC, Check Container Exists In Azure Blob Storage In C#.Net, Convert Dataset Datatable To Json Data Array In Asp.Net C#.Net & VB.Net Or How To Use JavaScriptSerializer To Convert Datatable To Json Data Array , How To Show Tooltip On Mouse Hover In Gridview In Using JQuery In ASP.Net C#.Net & VB.Net, SQL Script To Drop Multiple Tables,Procedures At Once , SQL Script To Drop Multiple Tables,Procedures At Once , Convert Dataset Datatable To Json Data Array, SQL Script To Drop Multiple Tables,Procedures At Once In Sql Server, and many articles in C#.Net,ASP.Net,VB.Net,Grid View,Javascript,jQuery,SQL Server and many other topics.

Before proceeding to explain about how to get data from web api should know how to create the web api project. Read this article and then come to this article.

In the previous article i have created two controller methods. they are

Controller MethodURI
GetAllEmployees/api/employees
GetEmployee/api/employee/id

For the GetEmployee method, the id in the URI is a placeholder. For example, to get the employee with ID of 2, the URI is api/employee/2.

Now do the right click on the project select add then new item as shown in figure.

How to get fetch data from ASP.Net WebAPI or how to query ASP.Net WebAPI.

In the Add New Item dialog, select the Web node under Visual C#, and then select the HTML Page item. Name the page "index.html" like shown in below.

How to get fetch data from ASP.Net WebAPI or how to query ASP.Net WebAPI.

In the index.html i have included the jQuery library. And the file have two tables one for showing all the employees and another one is for showing particular employee details. The html file has a input field to input employee id and a button. While clicking on the button it will fetch the details for that particular employee id and display details in the table. Below is the entire code.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Product App</title>
</head>
<body>

    <div>
        <h2>All Employees</h2>
        <table id="tblEmployees" border="1" style="border-collapse:collapse;"></table>
    </div>
    <div>
        <h2>Search by ID</h2>
        <input type="text" id="employeeId" size="5" />
        <input type="button" value="Search" onclick="find();" />
        <p id="product" />
        <table id="tblEmployee" border="1" style="border-collapse:collapse;"></table>
    </div>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script>
        var uri = 'api/employee';

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

        function formatItem(item) {
            return '<td>' + item.EmployeeId + '</td><td>' + item.EmployeeName + '</td><td>' + item.Department + '</td><td>' + item.Designation + '</td>';
        }
        function formHeader() {
            return '<th>EmployeeId </th><th>EmployeeName </th><th>Department </th><th>Designation</th>';
        }

        function find() {
            var id = $('#employeeId').val();
            $.getJSON(uri + '/' + id)
                .done(function (data) {
                    $('#tblEmployee').html(formHeader());
                    $('<tr>', { html: formatItem(data) }).appendTo($('#tblEmployee'));
                })
                .fail(function (jqXHR, textStatus, err) {
                    $('#tblEmployee').html('Error: ' + err);
                });
        }
    </script>
</body>
</html>


Now if you run the code you will get the below output.
Output of How to get fetch data from ASP.Net WebAPI or how to query ASP.Net WebAPI.

 Source code of How to get fetch data from the ASP.Net WebAPI or how to query ASP.Net WebAPI.

Hope you have enjoyed. Please share your comments and share this article.. Happy coding...

You May Also Like...


Comments

Popular posts from this blog

Code To Convert rupees(numbers) into words using C#.Net

Introduction: In my previous article I have explained about how to validate emailid using javascript . In this article I am going to explain about code used to convert rupees(numbers) into words using C#.Net . Explanation: For explanation purpose I have a page. It has a textbox to input the numbers. And when you click on the convert to words button then it will convert the input numbers into words and shows it in the below label. Below is the C# code used to do this functionality. public static string NumbersToWords( int inputNumber) {     int inputNo = inputNumber;     if (inputNo == 0)         return "Zero" ;     int [] numbers = new int [4];     int first = 0;     int u, h, t;     System.Text. StringBuilder sb = new System.Text. StringBuilder ();     if (inputNo < 0)     { ...

Puzzles for kids - 12 Days Of Christmas

According to the traditional song, on the first day of Christmas (25th December), my true love sent to me: . A partridge in a pair tree On the second day of Christmas (26th December), my true love sent to me THREE presents: . Two turtle doves . A partridge in a pear tree On the third day of Christmas (27th December and so on) my true love sent to me SIX presents: . Three French hens . Two turtle doves . A partridge in a pear tree This carries on until the the twelfth day of Christmas, when my true love sends me: Twelve drummers drumming Eleven pipers piping Ten lords a-leaping Nine ladies dancing Eight maids a-milking Seven swans a-swimming Six geese a-laying Five gold rings Four calling birds Three French hens Two turtle doves A partridge in a pear tree After the twelve days of Christmas are over, how many presents has my true love sent me altogether? Our Solution: 1 + 3 + 6 + 10 + 15 + 21 + 28 + 36 + 45 + 55 + 66 + 78 = 364 presents Which is really interesting when you think ...

Asp.Net DataTable Manipulation - Add, Update,Delete & Sort DataTable in C#.Net & VB.Net

In the asp.net developer's life it is very common to come across the datatable manipulation. So here i have decided to explain the datatable manipulation like adding records to datatable, editing and updating datatable records,deleting records from datatable based on the condition and finally sorting datatable based on columns.