Creating an ASP.NET Search Engine

How to integrateand Enhancing the code

How to integrate

The application has been tested with the web form SiteSearch.aspx in the root directory. So my suggestion is that you do the same. Later on, you can try moving it to any subfolder. All my classes I have placed in components folder. You can move them to any folder of your choice.

Errors

When the application is place in the root you may get the following errors. The remote server returned an error: (401) Unauthorized.
OR
The remote server returned an error: (500) Internal Server Error.

This error is caused because
1)If server returns (401) Unauthorized, the application is unable to read the file beacuse of right access.
2)If server returns (500) Internal Server Error, the page that it was trying to read returned an error. The page that application was trying to read either has an error or requires parameters because of which it returns error

Follow the steps to rectify the error
1)In the Web.config ensure that the BarredFolders list is comprehensive
aspnet_client,_private,_vti_cnf,_vti_log,_vti_pvt,_vti_script,_vti_txt,cgi_bin,_bin,bin,_notes,images,scripts
2)Ensure that the BarredFiles list is comprehensive and contains localstart.asp,iisstart.asp

Globalization

The Search Engine module can be easily globalize. For this purpose we will see how to convert it into Chinese language.

Web.config

The XML declaration must appear as the first line of the document without other content, including white space, in front of the start <.

The XML declaration in the document map consists of the following:<?xml version="1.0" encoding="Your Encoding" ?>. By default the visual studio uses the utf-8 encoding this needs to be changed to encoding that you want to use. Here we will change to gb2312. Hence the XML Declaration needs to be modified as follows.

English

<?xml version="1.0" encoding="utf-8" ?>

Chinese

<?xml version="1.0" encoding="gb2312" ?>

The requestEncoding and responseEncoding specifies the assumed encoding of each incoming request and outgoing response. The default encoding is UTF-8, specified in the <globalization> tag included in the Machine.config file created when the .NET Framework is installed. If encoding is not specified in a Machine.config or Web.config file, encoding defaults to the computer's Regional Options locale setting. We will need to change requestEncoding and responseEncoding to reflect the change in encoding.

English

<globalization requestEncoding="utf-8" responseEncoding="utf-8" />

Chinese

<globalization requestEncoding="gb2312" responseEncoding="gb2312" />

In order to avoid building the code when the encoding changes we need to add the encoding key to the appsettings.

<!-- Set this to the Encoding of the web site-->     
<add key="Encoding" value="gb2312" />    

Also change the English Language key to false.

 <!-- Set this boolean to False if you are not using 
  an English language web site--> 

  <add key="EnglishLanguage" value="True" /> 

SiteSearch.aspx

Last but not the least, the codepage attribute has to be added in the page directive.

English

<%@ Page Language="vb" Trace="False" AutoEventWireup="false" Codebehind="SiteSearch.aspx.vb" Inherits="SearchDotnet.SiteSearch" debug="false" %>

Chinese

<%@ Page Language="vb" Trace="False" AutoEventWireup="false" Codebehind="SiteSearch.aspx.vb" Inherits="SearchDotnet.SiteSearch" debug="false" codePage="936" %>

Enhancing the code

The application is meant for small sites. For bigger sites, the code can be further enhanced. Infact you will need to write to database say XML file periodically and then read from it. I will offer a few tips to do so.

1)In my code I search and filter the data using regular expression. Instead of this you will have to write entire data (not filtered data) to XML file using the following method.

      Private Shared Sub WriteXmlToFile(ByVal thisDataSet As DataSet)
            If thisDataSet Is Nothing Then
                Return
            End If

            thisDataSet.WriteXml(XMLFile)
        End Sub

2)Later you will need to read the xml from file save it to the shared dataset say Searchs.Site.PageDataset.Tables("Pages").

        Private Shared Function ReadXmlFromFile() As DataSet
            ' Create a new DataSet.
            Dim newDataSet As New DataSet("New DataSet")

            ' Read the XML document back in. 
            ' Create new FileStream to read schema with.
            Dim fsReadXml As New System.IO.FileStream(XMLFile, System.IO.FileMode.Open)

            ' Create an XmlTextReader to read the file.
            Dim myXmlReader As New System.Xml.XmlTextReader(fsReadXml)

            ' Read the XML document into the DataSet.
            newDataSet.ReadXml(myXmlReader)

            ' Close the XmlTextReader
            myXmlReader.Close()
            Return newDataSet

        End Function

3)For each search you will later need use the Select method of PageDataset.Tables to filter it according to search results. Filter dataset might look like this. FillDataset method contains logic to create and add search results (array of DataRow) to database.

       Private Sub FiterPagesDatset()
            Dim strExpr As String
            Dim foundRows As DataRow()

            Dim Field() As String = {"Title", "Description", "Keywords", "Contents"}

            strExpr = SomeFunction 'Your function to build the query.


            foundRows = Searchs.Site.PageDataset.Tables("Pages").Select(strExpr)
            FillDataset(foundRows)
        End Sub

4)The filtered result store it into another dataset and use it to display results

Points of Interest

When I was working on the project, the question was how to display the results. DataGrid was my choice as we can exploit a lot of its features which are not present in other list controls. Once my question was solved, the next was how to pass content to the DataGrid. DataSet was the only alternative. As I worked further before storing the information in DataSet, I had to move to and fro with bulk information about the page. I decided to use site object to store the information.

One of the authors suggested the following best practices:

  1. The class should be small to have approximately half a dozen methods and properties.
  2. Methods be short with again approximately half a dozen lines.

After carefully analyzing the code and keeping the best practices in mind, I redesigned the code to what it is now.

Histoy

Modified the code to read dynamic pages.

Enhanced the code to support globalization.

You might also like...

Comments

About the author

Stevan Rodrigues United States

I am a Microsoft Certified Solutions Developer in .Net Architecture (MCSD.Net Early Achiever – one among the first 2500 worldwide), Microsoft Certified Application Developer in .Net – MCAD.Net (...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“An expert is a man who has made all the mistakes that can be made in a very narrow field” - Niels Bohr