Click here to Skip to main content
15,893,904 members
Articles / Web Development / ASP.NET

Using Google Co-op's Custom Search Engine

Rate me:
Please Sign up or sign in to vote.
4.86/5 (23 votes)
19 Dec 2013CPOL5 min read 304K   5.8K   101  
This article shows how easy it is to use ASP.NET and Google Co-op's Custom Search Engine to build your own search engine.
using System;
using System.Data;
using System.Configuration;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
	#region Instance Methods -- Event Handlers

	/// <summary>
	/// When not a PostBack, if there is a query string parameter named "q" that is not empty, 
	/// put its contents in the "q" textbox so that the user can see what they last searched for.
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load (Object sender, EventArgs e)
    {
		if (!IsPostBack)
		{
			String query = Request.QueryString["q"];
			if (!String.IsNullOrEmpty (query))
			{
				q.Text = SanitizeUserInput (HttpUtility.UrlDecode (query.Trim ()));
			}
		}
	}

	/// <summary>
	/// Using the Google form field values, redirect to this page with those values in the URL's
	/// query string.  Google's JavaScript will pick up these parameters, perform the search, and
	/// display the results on your page.
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void _btnSearch_Click (Object sender, EventArgs e)
	{
		if (!IsValid)
			return;

		Response.Redirect (
			String.Format(
				"Default.aspx?q={0}&cx={1}&cof={2}",
				HttpUtility.UrlEncode (SanitizeUserInput (q.Text.Trim ())),
				HttpUtility.UrlEncode (cx.Value),
				HttpUtility.UrlEncode (cof.Value)
				),
			false
			);

		Context.ApplicationInstance.CompleteRequest ();
	}

	/// <summary>
	/// Strip tags from user input.
	/// </summary>
	/// <param name="text"></param>
	/// <returns></returns>
	private String SanitizeUserInput (String text)
	{
		if (String.IsNullOrEmpty (text))
			return String.Empty;

		String rxPattern = "<(?>\"[^\"]*\"|'[^']*'|[^'\">])*>";
		Regex rx = new Regex (rxPattern);
		String output = rx.Replace (text, String.Empty);

		return output;
	}

	#endregion
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Sagara Software, Inc.
United States United States
Jon is a senior software developer who loves using .NET to solve problems.

When he's not fooling around with computers or reading, he's busy spending time with his super wife, Kelly, and his three boys. He also likes to take his mountain bike for a spin.

Visit my blog

Comments and Discussions