8257 interfacing 2 in microprocessor for btech students
Creating web api and consuming part 2
1. Creating Asp.net Web API and
consuming it through HTML Clients
– Part II
Howdy Readers,
Consuming web API through HTML clients
Welcome to our blog ! In our previous post we have seen how to create ASP.NET Web API. If you
aren’t still comfortable in creating Web API then I would suggest you to read our post for creating
Web API.
Today, we will be focusing on consuming web API through HTML clients. Trust me guys, it’s very
easy to consume web API and we will do that using JQuery. If you are unfamiliar with JQuery then
please refer w3 schools for getting some basic idea about it. Latest version of Jquery can be
downloaded from here.
Modify the default file “index.cshtml” in the View/Home folder with the following Markup to provide
a GUI for Displaying Products and their different actions.
<!DOCTYPE html>
<html lang="en">
<body>
<header>
<script type="text/javascript" src="../../Scripts/jquery-1.8.2.js"></script>
</header>
<div id="body">
<section class="featured">
<div class="content-wrapper">
<hgroup class="title">
<h1>Welcome to ASP.NET Web API | Demo by <a href="http://dipendra
shekhawat.com" target="_blank">Dipendra Shekhawat</a></h1>
4. Creating ASP.NET Web API and consuming through html clients – 1
Now add the following script tag just after the </body> (closing body tag) to call web API
<script type="text/javascript">
var hostUrl = "http://" + document.location.hostname + ":" + window.location.port
;
var apiUrl = '/api/Product';
$(document).ready(function () { });
function GetAllProducts() {
$.ajax({
url: hostUrl + apiUrl,
type: 'GET',
5. dataType: 'json',
success: function (data) {
DisplayProductList(data);
},
error: function () {
alert('Some error occured');
}
});
}
function FindProduct() {
var id = $('#searchProductID').val();
$.getJSON(apiUrl + '/' + id)
.done(function (data) {
DisplayProductForEdit(data);
})
.fail(function (jqXHR, textStatus, err) {
$('#prod').text('Error: ' + err);
});
}
function DeleteProduct() {
var id = $('#delProductID').val();
$.ajax({
url: hostUrl + apiUrl + '/' + id,
type: 'DELETE',
dataType: 'json',
success: function (data) {
6. alert('Product deleted');
GetAllProducts();
},
error: function () {
alert('Product not found');
}
});
}
function UpdateProduct() {
var product = {
Id: $('#searchProductID').val(),
Name: $('#EditName').val(),
Type: $('#EditType').val(),
Description: $('#EditDescription').val(),
Price: $('#EditPrice').val()
};
$.ajax({
url: hostUrl + apiUrl,
type: 'PUT',
data: JSON.stringify(product),
contentType: "application/json;charset=utf-8",
success: function (data) {
alert('Employee updated');
GetAllEmployee();
},
error: function () {
alert('Some error occured');
8. var result = "<table><th>ID</th><th>Name</th><th>Type</th><th>Description</th
><th>Price</th>";
$.each(products, function (index, product) {
result += "<tr><td>" + product.Id + "</td><td> " + product.Name + "</td><
td>"
+ product.Type + "</td><td>" + product.Description + "</td><td>"
+ '₹ ' + product.Price + "</td></tr>";
});
result += "</table>";
$("#ProductList").html(result);
}
function DisplayProductForEdit(product) {
$("#prod").empty();
if (product == null) {
alert('Product Not Found')
return;
}
$("#prod").empty();
var result = "<table>" +
"<tr><td>Name</td><td><input type='text' id='EditName' value='" + product.Nam
e + "'</td></tr>" +
"<tr><td>Type</td><td><input type='text' id='EditType' value='" + product.Typ
e + "'</td></tr>" +
"<tr><td>Description</td><td><input type='text' id='EditDescription' value='
" + product.Description + "'</td></tr>" +
"<tr><td>Price</td><td><input type='text' id='EditPrice' value='" + product.P
rice + "'</td></tr>" +
"<tr><td colspan='3'align='center'><input type='button' value='Update' onclic
k='UpdateProduct();' </td></tr>" +
9. "</table>";
$("#prod").html(result);
}
</script>
Below are the screenshots for viewing all the products or finding a specific product.
Similarly, you can delete a product from the list by specifying its ID or add a new product.
Creating ASP.NET Web API and consuming through html clients – 2
10. Creating ASP.NET Web API and consuming through html clients – 3
Hurrah! now you have knowledge of creating ASP.NET Web API and consuming it through HTML
Clients. Don’t keep it to yourself. Please feel free to share your knowledge among your friends or
colleagues.