Introduction
This article explains you how to use the Google Search API in the ASP.NET Web API. Here we use a variable "sitesearch" that handles the Google Search engine. If we have the null value of the variable "sitesearch" then it searches the entire web and if we mention any site as the value of "sitesearch" then it searches only that site.
Google API:
- The Google API service is a beta web program that allows programmers to easily search and manage the information on the web.
- The Google API is used by developers and researchers only for finding information but it cannot be use as a resource in any application.
- It uses the SOAP (Simple Object Access Protocol) and WSDL (Web Services Description Language) to provide the interface between the program's user and the Google API.
- Different programming environment that are suitable for the Google API that are Visual Studio.NET, Java and so on.
- Developers and the researcher use the Google API to examine the request; it can be more than 3 billion web pages that are allowed by the Google API.
Let us see the procedure to use the Google API in the Web API.
- First we create a Web API Application.
- Start Visual Studio 2012.
- From the start window Select "Installed" -> "Visual C#" -> "Web".
- Select "ASP.NET MVC4 Web Application" and click the "OK" button.
- From the "MVC4 Project" window select "Web API".
- Now we add some code in the "Index.cshtml" file for using the Google API. This file exists:
- In the "Solution Explorer".
- Expand the "Home" folder.
- Then select the "index.cshtml" file.
Add this code:
<!DOCTYPE>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>c-sharpcorner</title>
</head>
<body>
<form method="get" action="http://www.google.com/search">
<input type="text" name="q" size="40" maxlength="255" value="" />
<input type="submit" value="Search" />
<br /> <input type="radio" name="sitesearch" value="" />
Search on Web
<input type="radio" name="sitesearch" value="c-sharpcorner.com" checked />
Search from c-sharpcorner<br />
<input type="radio" name="sitesearch" value="msdn.microsoft.com" checked />
search on MSDN
</form>
</body>
</html>
In the code above we have three radio buttons and three "sitesearch" variables. From which the first variable has a null value, in other words when we search for soomething it will search the entire web, the second has the site "c-sharpcorner.com" if we choose this radio button and find the information then it will search only in the site "c-sharpcorner.com". The last one has the value "msdn.microsoft.com"; when it is chosen only the "MSDN" site is searched for the information.
- Now to execute the application. Press "F5" and check that it is working. The output will be as in the following:
If we choose "Search on Web" then the output will be:
If we choose the site "c-sharpcorner.com" then the output will be:
If we choose the site "msdn.microsoft.com" then the output will be: