Friday, April 18, 2008

Using WrenSoft's ZoomSearch engine on an ASP.NET Web site

Wrensoft makes a very affordable and easy to use search engine that you can plug into your existing Web site. There are a few versions that are supported directly (ASP, CGI, and Javascript), but you can also use it on an ASP.NET Web site.

There are some instructions located on Wrensoft's website, which will help you build a basic search.aspx page for querying your website. However, if you want to integrate the search form into your own theme or site with masterpages, you'll need to take another approach.

The key is to use an asp:Literal control, and place it wherever you'd like the search results to appear. You can then build your search.aspx page just like any other themed page on your site and consider the Literal control to act like a 'Search results' control.

Using the code provided by wrensoft we'll write the output to that Literal control instead of the Response stream.

First, add your literal wherever you want the results to appear.

   1: <p>
   2:     <asp:Literal ID="ZoomSearch" runat="server"></asp:Literal>
   3: </p>

Then insert the code below (written in VB) in the Page_Load event. Note the code is copied from wrensoft's page, but I've changed the last line to take the results from the CGI search and place them into the asp:Literal

   1: 'Code from Wrensoft.com:  http://www.wrensoft.com/zoom/support/aspdotnet.html#vbdotnet
   2:  
   3:         Dim paramStr As String = ""
   4:         Dim parampos As Integer = Request.RawUrl.IndexOf("?")
   5:         If (parampos >= 0) Then
   6:             paramStr = Request.RawUrl.Substring((parampos + 1))
   7:         End If
   8:         Dim psi As ProcessStartInfo = New ProcessStartInfo
   9:         psi.FileName = Server.MapPath("~/search/search.cgi")
  10:         psi.EnvironmentVariables("REQUEST_METHOD") = "GET"
  11:         psi.EnvironmentVariables("QUERY_STRING") = paramStr
  12:         psi.EnvironmentVariables("REMOTE_ADDR") = Request.ServerVariables("REMOTE_ADDR")
  13:         psi.RedirectStandardInput = False
  14:         psi.RedirectStandardOutput = True
  15:         psi.UseShellExecute = False
  16:         Dim proc As Process = Process.Start(psi)
  17:         proc.StandardOutput.ReadLine()
  18:         Dim zoom_results As String = proc.StandardOutput.ReadToEnd
  19:         ' read from stdout 
  20:         proc.WaitForExit()
  21:         ' Print the output 
  22:         Me.ZoomSearch.Text = zoom_results

No comments: