Using Google Search API in ASP.Net Web API

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.

  1. 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.

      google.jpg
    • From the "MVC4 Project" window select "Web API".

      google1.jpg

    • Click the "OK" button.
  1. 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.
      google2.jpg
    • Add this code:
      1. <!DOCTYPE>  
      2. <html xmlns="http://www.w3.org/1999/xhtml">   
      3. <head>  
      4.     <title>c-sharpcorner</title>  
      5. </head>  
      6. <body>  
      7.     <form method="get" action="http://www.google.com/search">  
      8.     <input type="text" name="q" size="40" maxlength="255" value="" />  
      9.     <input type="submit" value="Search" />  
      10.    <br /> <input type="radio" name="sitesearch" value="" />  
      11.     Search on Web  
      12.     <input type="radio" name="sitesearch" value="c-sharpcorner.com" checked />  
      13.     Search from c-sharpcorner<br />  
      14.           <input type="radio" name="sitesearch" value="msdn.microsoft.com" checked />  
      15. search on MSDN  
      16.     </form>  
      17. </body>  
      18. </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.
  1. Now to execute the application. Press "F5" and check that it is working. The output will be as in the following:

    google3.jpg

    If we choose "Search on Web" then the output will be:

    google6.jpg
    If we choose the site "c-sharpcorner.com" then the output will be:

    google5.jpg

    If we choose the site "msdn.microsoft.com" then the output will be:

    google4.jpg


Similar Articles