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

Sort Dictionary Based On Value In Asp.Net And C#.Net | Convert Dictionary into KeyValuePair or KeyValuePair into Dictionary.

In this tutorial i am going to explain about how to sort dictionary object based on value in asp.net and C#.Net or convert unsorted dictionary to sorted dictionary object in C#.Net and VB.Net or Convert Dictionary into KeyValuePair or KeyValuePair into Dictionary.

Geckofx Browser in Winform Application

Bored with IE browser in your winform application ? Want to do everything as you doing in your Firefox or Chrome Browser ? Play with automation ? Then here is your choice . Introduction:  GeckoFX is a Windows Forms control written in clean, commented C# that embeds the Mozilla Gecko browser control in any Windows Forms Application. It also contains a simple class model providing access to the HTML and CSS DOM . GeckoFX was originally created by Andrew Young for the fast-growing visual CSS editor, Stylizer . It is now released as open-source under the Mozilla Public License.  You can download it here :   Geckofx 22.0 And the supporting file Xulrunner here :   Xulrunner Files Hope you have downloaded above two files. Here our journey going to start. Create your winform application in visual studio and do the following: right click the toolbox -> Choose items -> Browse the "Geckofx-winforms.dll" and click "yes" for “Load it anyw...

Code to create log files in C#.Net|Asp.Net

Introduction: In my previous article I have explained about how to create, delete and check whether the directory exists using C#.Net . In this article I am going to explain about  How to create log files in C#.Net. Explanation: Log files are useful to track any runtime errors and exceptions in all the applications. Below code will code will get the Message and Pagename as the input and creates the log file in that date. For that first i have imported below two namespaces.