In this article we will explain a topic that is slighlty more advanced than previous articles. Here we will try to consume a WEB-API service using a jQuery Ajax function. Since this article is not a complete tutorial of the WEB-API, it demands some basic concepts and understanding of the Web-API and it's uses. Try to understand the implementation.
Step 1: Create simple MVC-4 Web API application
To create a WEB-API we need to create one MVC-4 application in Visual Studio. Here is a sample screen to create the MVC application.
Choose "MVC 4 Web Application" and proceed. Next you will get the following screen. Here we will choose "WEB-API".
Let's keep the default Razor view engine and proceed. Once the application has been crated you will find a complete nearly empty structure of Web API application. Within the Controller folder you will find a few empty controllers. Here we have chosen the Values controller to demonstrate this simple example.
Here is our sample controller class. You can see that the controller class is inherited from the ApiController class. Within this controller class we have defined one simple function that can be accessed using the GET verb. This Get() method can be consumed by a jQuery Ajax method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace TestAPI.Controllers
{
public class ValuesController : ApiController
{
// GET api/values
public String Get()
{
return "Hello world";
}
}
}
This method is very simple and we are returning a string from here. Now, the Web-API configuration is over and we will create client code to consume it.
Step 2: Create jQuery Ajax method to consume Web-API method
We will create one .aspx page to define a jQuery Ajax method. So, add one Web page in the same solution and put the following code in the HTML view.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Consumer.aspx.cs" Inherits="TestAPI.Consumer" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script src="Scripts/jquery-1.7.1.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var url = 'api/Values';
$.ajax({
url: url,
type: 'GET',
success: function (result) {
var r = JSON.stringify(result);
alert("From Web-API " + r);
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
</form>
</body>
</html>
We are consuming the Web-API service via jQuery Ajax function. Try to look at the URL variable that was defined within the head section. We have specified the URL with the following
API/Values.
The Values is nothing but the name of the controller class. Here is sample output of the application.
The fundamental idea
In this article series we saw how to consume a service method from code-behind, how to consume a traditional Web Service, WCF service and Web-API using jQuery Ajax method. Hope you have understood the entire concept. Let me ask one question. Why does all those various services exist in the market?
The answer is men are greedy by nature. Ha..Ha.. No, this is not an industrial standard answer. The answer is there is a limitation of various services. One cannot fulfill all needs.
- Think about Web Service
In a traditional Web Service the communication protocol is SOAP. The message passing language (in the broad sense, let's consider it as a language) is XML. And each time a SOAP request and response is transfered, the unnecessary data is transfered between client and server. So, this is one limitation of a Web Service.
- WCF Service
Then Microsoft gave us WCF as a solution. It's very fine , supports XML and JSON as data format and the big advantage is that it is RESTful service.
- Then why Web-API?
If you read the WCF portion of this series then you know how many places we have changed/modified to run our service. So, in WCF many settings are needed. The latest solution is Web-API. It's RESTful in nature and nearly no settings are needed. All types of applications can be consumed by it in various message formats and many more.
No comments:
Post a Comment