Create site search engine using Asp.net with C Sharp

Create site search engine using Asp.net with C#

The Search Engine module will search an entire page also dynamic pages for matching keyword(s) or a phrase and will count how many times the keyword(s) or phrase are found on the page, and display the results with the highest matches first. The module will search all files with the extensions that you can easily place the extension name into the web.config file where indicated. Files or folders that you don't want searched can be placed in the web.config where indicated, so these files and folders are not searched.

 

The ability to define a class and create instances of classes is one of the most important capabilities of any object-oriented language. In the coming section, we see the classes that we have used in the search module.

 

Class Name

Description

SearchControl

Class for the search elements.

SiteSearchResults

Class for the web form where the user can search the site for certain words and for display results.

CleanHtml       

Class to clean the HTMl content

FileContent

Class to get the content form the HTML File

Page

Class to store data of the pages

PagesDataset

Class to create and store results in dataset

MySite

Class to read the configurations of the site

UserSearch

Class to store the search information per user

 

 

SearchControl.ascx

 

Web controls are one of the new, exciting features in Microsoft's .NET initiative. SearchControl.ascx is a web control int this page only the necessary elements, a Textbox and radio buttons for search criteria, are  placed.

 

ASP.NET is an event-driven programming environment. The Search buttun click event redirects to the SiteSearchResults.aspx page.

 

 

protected void srchbtn_Click(object sender, EventArgs e)

    {

        //Getting the search criteria.

        string criteria = "";

        if (Phrase.Checked)

        {

            criteria = "phrase";

        }

        else if (AllWords.Checked)

        {

            criteria = "allwords";

        }

        else

        {

            criteria = "anywords";

        }

        //Check any search value is entered.

        if (txtSearch.Text.Trim() != "")

        {

            Response.Redirect("SiteSearchResults.aspx?criteria="+criteria+"&str="+txtSearch.Text);

        }

    }

 

 

SiteSearchResults.aspx

 

Web Forms are one of the new, exciting features in Microsoft's .NET initiative. SiteSearchResults.aspx is a web form which is also the start page for the search module.

 

A Web Forms page consists of a page (ASPX file) and a code behind file (.aspx.cs file or .aspx.vb file). Our web form consists of SiteSearchResults.aspx and SiteSearchResults.aspx.cs. I will be treating them simultaneously touching on the main elements of the web form.

Page_Load

 

The server controls are loaded on the Page object and the view state information is available at this point. The Page_Load event checks the search string and search criteria.If any search words then SearchSite function is called. The DataGrid content is set by calling the BindDataGrid method. ViewState["SortExpression"] is used to store the sort expression.

 

private void Page_Load(object sender, System.EventArgs e)

    {

        string searchCriteria = Request.QueryString["criteria"];

        string strSearchWords = Request.QueryString["str"];

        pnlSearchResults.Visible = false;

        if (strSearchWords == null) strSearchWords = "";

        if (searchCriteria == null) searchCriteria = "";

        //If there is no words entered by the user to search for then dont carryout the file search routine

        if (!strSearchWords.Equals(""))

        {

            MySite.ApplicationPath = string.Format("http://{0}{1}", Request.ServerVariables["HTTP_HOST"], Request.ApplicationPath);

            sSite = SearchSite(strSearchWords, searchCriteria);

            if ((sSite.PageDataset != null))

            {

                pnlSearchResults.Visible = true;

                lblSearchWords.Text = sSite.SearchWords;

 

                if (ViewState["SortExpression"] == null)

                {

                    ViewState["SortExpression"] = "MatchCount Desc";

                }

                BindDataGrid(ViewState["SortExpression"].ToString());

                lblTotalFiles.Text = sSite.TotalFilesSearched.ToString();

                lblFilesFound.Text = sSite.TotalFilesFound.ToString();

            }

        }

    }

 

SearchSite

 

The main call to the search takes place in this method. UserSearch class which we will cover shortly stores the entire search information and the result of the search. UserSearch object, i.e. srchSite is created and its properties like SearchWords and SearchCriteria assigned. Also srchSite.Search method is called.

 

// SearchSite method

    private UserSearch SearchSite(string strSearch,string searchCriteria)

    {

        UserSearch srchSite = default(UserSearch);

        srchSite = new UserSearch();

        //Read in all the search words into one variable

        srchSite.SearchWords = strSearch;

 

        if (searchCriteria=="phrase")

        {

            srchSite.SearchCriteria = SearchCriteria.Phrase;

        }

        else if (searchCriteria == "allwords")

        {

            srchSite.SearchCriteria = SearchCriteria.AllWords;

        }

        else

        {

            srchSite.SearchCriteria = SearchCriteria.AnyWords;

        }

        srchSite.Search(Server.MapPath("./"));

        return srchSite;

    }

 

DataGrid

 

The DataGrid control renders a multi-column, fully templated grid and is by far the most versatile of all data bound controls. Moreover the DataGrid control is the ASP.NET control of choice for data reporting. Hence, I have used it to display the search results. Since the focus of the article is the internal search engine, I will just give a brief overview of the features of the DataGrid used here.

 

The content of a DataGrid is set by using its DataSource property. The entire search result is stored in the sSite.PageDataset.Tables["Pages"]. Hence the content of the DataGrid is set to dvwPages i.e. sSite.PageDataset.Tables["Pages"].DefaultView. BindDataGrid method is called every time the page loads.

 

    // BindDataGrid method

    private void BindDataGrid(string strSortField)

    {

        DataView dvwPages = default(DataView);

        dvwPages = sSite.PageDataset.Tables["Pages"].DefaultView;

        dvwPages.Sort = strSortField;

        dgrdPages.DataSource = dvwPages;

        dgrdPages.DataBind();

    }

The control has the ability to automatically generate columns that are based on the structure of the data source. Auto-generation is the default behavior of DataGrid, but you can manipulate that behavior by using a Boolean property named AutoGenerateColumns. Set the property to False when you want the control to display only the columns you explicitly add to the Columns collection. Set it to True (the default) when you want the control to add as many columns as is required by the data source. Auto-generation does not let you specify the header text, nor does it provide text formatting. Hence, here I have set it to False. You typically bind columns using the <columns> tag in the body of the <asp:datagrid> server control.

 

<Columns>

                                    <asp:TemplateColumn>

                                          <ItemTemplate>

                                                <%# DisplayTitle(Eval("Title").ToString(),Eval( "Path" ).ToString()) %>

                                                <br>

                                                <%# Eval( "Description" ) %>

                                                <br>

                                                <span class="Path">

                                                      <%# String.Format("{0} - {1}kb", DisplayPath(Eval( "Path" ).ToString()),Eval( "Size" ).ToString()) %>

                                                </span>

                                                <br>

                                                <br>

                                          </ItemTemplate>

                                    </asp:TemplateColumn>

                              </Columns>

 

DisplayTitle method and DisplayPath method are used to display customized information to the columns in the DataGrid

 

// DisplayTitle method

    // Display title of searched pages

    protected string DisplayTitle(string Title, string Path)

    {

        return string.Format("<A href='{1}'>{0}</a>", Title, Path);

    }

    // DisplayPath method

    // Path of the file is returned

    protected string DisplayPath(string Path)

    {

        return string.Format("{0}{1}/{2}", Request.ServerVariables["HTTP_HOST"], Request.ApplicationPath, Path);

    }

 

 

Page.cs

 

 

The role of the Page object is to store the data related to each page of the site.

 

The Page class defines the following properties

Path:- Stores the path of the file

Title:- Stores the text in HTML title tag

Keywords:- Stores the text in HTML meta keywords tags

Description:- Stores the text in HTML meta description tags

Contents:- Stores the text in HTML page

Matchcount:- Stores the matches found in HTML page

 

 

 

    private string _path = "";

    private string _title = "";

    private string _keywords = "";

    private string _description = "";

    private string _contents = "";

    private int _matchcount = 0;

    private decimal _size = 0;

 

    // Assign and retreive Size of the file

    public decimal Size

    {

        get { return _size; }

        set { _size = value; }

    }

 

    // Assign and retreive path of the file

    public string Path

    {

        get { return _path; }

        set { _path = value; }

    }

 

    //Assign and retreive title of the file

    public string Title

    {

        get { return _title; }

        set { _title = value; }

    }

 

    // Assign and retreive Keywords ( meta tags ) of the file

    public string Keywords

    {

        get { return _keywords; }

        set { _keywords = value; }

    }

 

    // Assign and retreive description ( meta tags ) of the file

    public string Description

    {

        get { return _description; }

        set { _description = value; }

    }

 

    // Assign and retreive contents of the file

    public string Contents

    {

        get { return _contents; }

        set { _contents = value; }

    }

 

 

    // Assign and retreive MatchCount of the file

    public int MatchCount

    {

        get { return _matchcount; }

        set { _matchcount = value; }

    }

 

The Page class has two private methods and two public methods. It defines the following methods:

 

CheckFileInfo Method

 

It is a public method which checks if title, description and content exists. If the text for title is empty then it assigns the default value "No Title". If the text for description is empty then it either assigns the contents of the file or default value "There is no description available for this page".

 

// function to the check the file contains title and decription

    public void CheckFileInfo()

    {

 

        //If the page contains no title then Page Title variable the appropriate message to display

        if ((_title == null) | _title.Trim().Equals(""))

        {

            _title = "No Title";

        }

        //If the page contains no title then Page Description variable the appropriate message to display

        if ((_description == null) | _description.Trim().Equals(""))

        {

            if ((_contents == null) | _contents.Trim().Equals(""))

            {

                _description = "There is no description available for this page";

            }

            else

            {

                if (_contents.Length > 200)

                {

                    _description = _contents.Substring(0, 200);

                }

                else

                {

                    _description = _contents;

                }

            }

        }

    }

 

Search method

 

Search method is a public method which calls SearchPhrase and SearchWords methods depending on the search criteria. SearchPhrase method searches for phrases while SearchWords searches for all or any words. Both these methods calls SearchPattern metod which uses regular expressions to search the files.

   // function to the search file

    public void Search(string strSearchWords, SearchCriteria SrchCriteria)

    {

 

        //If the user has choosen to search by phrase

        if (SrchCriteria == SearchCriteria.Phrase)

        {

            SearchPhrase(strSearchWords);

        }

        //Else the search is either by all or any words

        else

        {

            SearchWords(strSearchWords, SrchCriteria);

        }

 

    }

// Function to the search file

    private void SearchPhrase(string strSearchWords)

    {

        Regex regexp = default(Regex);

        MatchCollection mtches = default(MatchCollection);

 

        //Set the pattern to search for

        regexp = new Regex("", RegexOptions.IgnoreCase);

 

        //Search the file for the phrase

        mtches = Regex.Matches(_contents, string.Format("\\b{0}\\b", strSearchWords), RegexOptions.IgnoreCase);

 

        //Check to see if the phrase has been found

        if (mtches.Count > 0)

        {

            //Get the number of times the phrase is matched

            _matchcount = mtches.Count;

        }

    }

 

    // Function to the search file

    private void SearchWords(string strSearchWords, SearchCriteria SrchCriteria)

    {

        Regex regexp = default(Regex);

        int intSearchLoopCounter = 0;

        string[] sarySearchWord = null;

        //Array to hold the words to be searched for

        MatchCollection mtches = default(MatchCollection);

 

        //Split each word to be searched up and place in an array

        sarySearchWord = strSearchWords.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

 

        regexp = new Regex("", RegexOptions.IgnoreCase);

 

        //Loop round to search for each word to be searched

        for (intSearchLoopCounter = 0; intSearchLoopCounter < sarySearchWord.Length; intSearchLoopCounter++)

        {

 

            //Set the pattern to search for

            mtches = Regex.Matches((_contents), string.Format("\\b{0}\\b", sarySearchWord[intSearchLoopCounter]), RegexOptions.IgnoreCase);

 

            if (SrchCriteria == SearchCriteria.AnyWords)

            {

                _matchcount = _matchcount + mtches.Count;

            }

            else if (SrchCriteria == SearchCriteria.AllWords)

            {

                //Check to see if any of the words have been found

                if (mtches.Count > 0)

                {

                    //Get the number of times the search word is matched

 

                    if (_matchcount == 0 | (_matchcount > 0 & _matchcount > mtches.Count))

                    {

                        _matchcount = mtches.Count;

                    }

                }

                else

                {

                    //If the search word is not found then set the search found variable back to false as one of the words has not been found

                    _matchcount = 0;

                    return;

                }

 

            }

        }

    }

 

 

USerSearch.cs

 

 

It contains the following properties:

SearchCriteria:- The user choice of search is stored and retrieved from here

SearchWords:- The search words used by the user is stored and retrieved from here

TotalFilesSearched:- Total Files Searched is read from here

TotalFilesFound:- Total Files Searched found is read from here          

 

private int _totalFilesSearched = 0;

    private int _totalFilesFound = 0;

    private SearchCriteria _searchCriteria = SearchCriteria.AllWords;

    private string _searchWords = "";

    private DataSet _dstPages;

    private System.Web.UI.Page _page = new System.Web.UI.Page();

 

    // Retreive TotalFilesSearched of the site

    public int TotalFilesSearched

    {

        get { return _totalFilesSearched; }

    }

 

    // Retreive TotalFilesFound of the site

    public int TotalFilesFound

    {

        get { return _totalFilesFound; }

    }

 

    // Retreive data of tzh entire site of the site

    public DataSet PageDataset

    {

        get { return _dstPages; }

    }

 

    // Assign and retreive SearchCriteria of the site

    public SearchCriteria SearchCriteria

    {

        get { return _searchCriteria; }

        set { _searchCriteria = value; }

    }

 

    //Assign and retreive SearchWords of the site

    public string SearchWords

    {

        get { return _searchWords; }

        set { _searchWords = value; }

    }

 

Search Method

 

Actual processing of the search begins here. DataSet to store the search results is created here and ProcessDirectory method is called

 

// Search the entire site

    public DataSet Search(string targetDirectory)

    {

        //If the site is in English then use the server HTML encode method

        if (MySite.EnglishLanguage == "True")

        {

            //Replace any HTML tags with the HTML codes for the same characters (stops people entering HTML tags)

            _searchWords = _page.Server.HtmlEncode(_searchWords);

        }

        //If the site is not english just change the script tags

        else

        {

            //Just replace the script tag <> with HTML encoded &lt; and &gt;

            _searchWords = _searchWords.Replace("<", "&lt;");

            _searchWords = _searchWords.Replace(">", "&gt;");

        }

        if (_dstPages == null)

        {

            _dstPages = PagesDataset.Create();

        }

        ProcessDirectory(targetDirectory);

        return _dstPages;

    }

 

ProcessDirectory Method

 

The ProcessDirectory loops through all the files and calls the ProcessFile method. Later, it also loops through the subdirectories and calls itself.

 

// Files in the directories are searched

    private void ProcessDirectory(string targetDirectory)

    {

        string[] fileEntries = null;

        string[] subdirectoryEntries = null;

        fileEntries = Directory.GetFiles(targetDirectory);

        // Process the list of files found in the directory

        foreach (string filePath in fileEntries)

        {

            _totalFilesSearched += 1;

            ProcessFile(filePath);

        }

        subdirectoryEntries = Directory.GetDirectories(targetDirectory);

        // Recurse into subdirectories of this directory

        foreach (string subdirectory in subdirectoryEntries)

        {

            //Check to make sure the folder about to be searched is not a barred folder if it is then don't search

            if (!MySite.BarredFolders.Contains(Path.GetFileName(subdirectory)))

            {

                //Call the search sub prcedure to search the web site

                ProcessDirectory(subdirectory);

            }

        }

 

    }

 

ProcessFile Method

 

The ProcessFile calls the GetInfo which returns the Searchs.Page object which contains all the information of the particular file. Later, it checks if the matchcount is greater than 0 and calls the CheckFileInfo to clean up the information stored in the Page object. It then stores the file in the PagesDataset.

 

// Real logic for processing found files would go here.

    private void ProcessFile(string FPath)

    {

        Page srchFile = default(Page);

 

        srchFile = GetInfo(FPath);

        if ((srchFile != null))

        {

 

            srchFile.Search(_searchWords, _searchCriteria);

            if (srchFile.MatchCount > 0)

            {

                _totalFilesFound += 1;

                //Response.Write(srchFile.Contents)

                srchFile.CheckFileInfo();

                PagesDataset.StoreFile(_dstPages, srchFile);

            }

        }

    }

 

GetInfo Method

 

The GetInfo method's main task is to get the data of the file. It calls the shared method Searchs.FileContent.GetFileInfo where much of the work is done.

 

// File data is picked in this method

    private Page GetInfo(string FPath)

    {

 

        FileInfo fileInform = new FileInfo(FPath);

        StreamReader sr = default(StreamReader);

        Page srchFile = new Page();

        StringBuilder strBldFile = new StringBuilder();

        string strFileURL = null;

        //Holds the path to the file on the site

 

        //Check the file extension to make sure the file is of the extension type to be searched

        if (MySite.FilesTypesToSearch.Contains(fileInform.Extension))

        {

            //Check to make sure the file about to be searched is not a barred file if it is don't search the file

            if (!MySite.BarredFiles.Contains(Path.GetFileName(FPath)))

            {

 

                if (!File.Exists(FPath))

                {

                    return null;

                }

 

                GetFileInfo(FPath, srchFile);

 

                return srchFile;

            }

 

        }

        return null;

    }

 

 

GetFileInfo Method

 

Here the chunk of the data in the page is retrieved. The file content is either read from the source if the files are static using the GetStaticFileContent method. If the files are dynamic then content is retreived from server using the GetDynamicFileContent method. Title information is retrieved from the title tags, and description and keywords from meta tags by calling the GetMetaContent method. Contents of the file is stripped from the HTML page by calling Searchs.CleanHtml.Clean method.

 

  // File data is picked in this method

    private void GetFileInfo(string FPath, Page srchFile)

    {

        FileInfo fileInform = new FileInfo(FPath);

        StringBuilder strBldFile = new StringBuilder();

        decimal fileSize = fileInform.Length / 1024;

        srchFile.Size = fileSize;

 

 

        GetFilePath(FPath, srchFile);

 

        if (MySite.DynamicFilesTypesToSearch.Contains(fileInform.Extension))

        {

            _page.Trace.Warn("Path", string.Format("{0}/{1}", "", srchFile.Path));

            GetDynamicFileContent(srchFile);

        }

        else

        {

            GetStaticFileContent(FPath, srchFile);

        }

 

 

        if (!srchFile.Contents.Equals(""))

        {

            //Read in the title of the file

            srchFile.Title = GetMetaContent(srchFile.Contents, "<title>", "</title>");

 

            //Read in the description meta tag of the file

            srchFile.Description = GetMetaContent(srchFile.Contents, "<meta name=\"description\" content=\"", "\">");

            //_page.Trace.Warn("Page Desc", strPageDescription)

 

            //Read in the keywords of the file

            srchFile.Keywords = GetMetaContent(srchFile.Contents, "<meta name=\"keywords\" content=\"", "\">");

 

            srchFile.Contents = CleanHtml.Clean(srchFile.Contents);

 

            srchFile.Contents = strBldFile.AppendFormat("{0} {1} {2} {3}", srchFile.Contents, srchFile.Description, srchFile.Keywords, srchFile.Title).ToString().Trim();

        }

 

    }

 

// File Content is picked in this method

    private void GetStaticFileContent(string FPath, Page srchFile)

    {

        StreamReader sr = default(StreamReader);

 

        sr = File.OpenText(FPath);

 

        try

        {

            srchFile.Contents = sr.ReadToEnd();

            sr.Close();

        }

        catch (Exception ex)

        {

        }

    }

 

GetDynamicFileContent

 

GetDynamicFileContent branches into two method viz GetDynamicFileContentOther or GetDynamicFileContentUTF depending on the encoding.

 

// File Content is picked in this method

    private void GetDynamicFileContent(Page srchFile)

    {

        System.Net.WebClient wcMicrosoft = default(System.Net.WebClient);

        UTF8Encoding objUTF8Encoding = default(UTF8Encoding);

 

        wcMicrosoft = new System.Net.WebClient();

        objUTF8Encoding = new UTF8Encoding();

        srchFile.Contents = objUTF8Encoding.GetString(wcMicrosoft.DownloadData(string.Format("{0}/{1}", MySite.ApplicationPath, srchFile.Path)));

 

    }

 

GetMetaContent Method

 

GetMetaContent method uses regular expressions to strip the tags and get the required information.

 

  // Metacontent is stripped in this method

    private string GetMetaContent(string strFile, string strMetaStart, string strMetaEnd)

    {

        //List the text between the title tags:

        Regex regexp = default(Regex);

        string strMeta = null;

        string strPattern = null;

        string strInPattern = null;

 

        //If no description or keywords are found then you may be using http-equiv= instead of name= in your meta tags

        if (strFile.ToLower().Contains(strMetaStart.ToLower()) == false & strMetaStart.ToLower().Contains("name="))

        {

            //Swap name= for http-equiv=

            strMetaStart = strMetaStart.Replace("name=", "http-equiv=");

        }

 

        //Build Pattern

        strInPattern = "((.|\\n)*?)";

        strPattern = string.Format("{0}{1}{2}", strMetaStart, strInPattern, strMetaEnd);

        regexp = new Regex(strPattern, RegexOptions.IgnoreCase);

 

        //Match Pattern

        strMeta = regexp.Match(strFile).ToString();

        //Build Pattern

        strInPattern = "(.*?)";

        strPattern = string.Format("{0}{1}{2}", strMetaStart, strInPattern, strMetaEnd);

 

        //Get Pattern content

        strMeta = Regex.Replace(strMeta, strPattern, "$1", RegexOptions.IgnoreCase);

 

        if (strMeta.StartsWith(strMetaStart) && strMeta.EndsWith(strMetaEnd))

        {

            strMeta = strMeta.Replace(strMetaStart, "");

            strMeta = strMeta.Replace(strMetaEnd, "");

        }

 

        return strMeta;

    }

 

PagesDataset.cs

 

This class is used to create and build the DataSet. It consists of two methods and StoreFile. Create method creates the DataSet to store the searched results and the Storefile is responsible for adding records to DataTable in the DataSet.

 

    // Creates a datset for the pages and returns the result

    public static DataSet Create()

    {

        //Objects are defined

        DataSet pgDataSet = new DataSet();

        DataColumn[] keys = new DataColumn[2];

 

        //Table is created and added to table collection

        pgDataSet.Tables.Add(new DataTable("Pages"));

 

        //Schema of table is defined

        pgDataSet.Tables["Pages"].Columns.Add("PageId", System.Type.GetType("System.Int32"));

        pgDataSet.Tables["Pages"].Columns.Add("Title", System.Type.GetType("System.String"));

        pgDataSet.Tables["Pages"].Columns.Add("Description", System.Type.GetType("System.String"));

        pgDataSet.Tables["Pages"].Columns.Add("Path", System.Type.GetType("System.String"));

        pgDataSet.Tables["Pages"].Columns.Add("MatchCount", System.Type.GetType("System.Int32"));

        pgDataSet.Tables["Pages"].Columns.Add("Size", System.Type.GetType("System.Decimal"));

 

        //PageId is defined as indentity

        pgDataSet.Tables["Pages"].Columns["PageID"].AutoIncrement = true;

        pgDataSet.Tables["Pages"].Columns["PageID"].AutoIncrementSeed = 1;

 

        //PageId is defined as the primary key

        keys[0] = pgDataSet.Tables["Pages"].Columns["PageId"];

        pgDataSet.Tables["Pages"].PrimaryKey = keys;

 

        return pgDataSet;

    }

 

    // Creates a datset for the pages and returns the result

    public static void StoreFile(DataSet dstPgs, Page srchPg)

    {

        //Objects are defined

        DataRow pageRow = default(DataRow);

 

        //New row is created

        pageRow = dstPgs.Tables["Pages"].NewRow();

 

        //Data is added

        pageRow["Title"] = srchPg.Title;

        pageRow["Description"] = srchPg.Description;

        pageRow["Path"] = srchPg.Path;

        pageRow["MatchCount"] = srchPg.MatchCount;

        pageRow["Size"] = srchPg.Size;

 

        //Row is added to the dataset

        dstPgs.Tables["Pages"].Rows.Add(pageRow);

    }

 

CleanHtml.cs

 

CleanHtml class contains a single public shared method which uses regular expressions to clean the HTML content.

 

public class CleanHtml

{

    public static string Clean(string Contents)

    {

        string strPattern = null;

        strPattern = "";

        Regex regexp = new Regex(strPattern, RegexOptions.IgnoreCase);

        Contents = Regex.Replace(Contents, "<(select|option|script|style|title)(.*?)>((.|\\n)*?)</(select|option|script|style|title)>", " ", RegexOptions.IgnoreCase);

        Contents = Regex.Replace(Contents, "&(nbsp|quot|copy);", "");

        Contents = Regex.Replace(Contents, "<([\\s\\S])+?>", " ", RegexOptions.IgnoreCase).Replace(" ", " ");

        Contents = Regex.Replace(Contents, "\\W", " ");

        return Contents;

    }

}

 

MySite.cs

 

MySite class consists of shared properties which store the configurations of the entire site. These properties get their values from the web.config file using the ConfigurationSettings.AppSettings.

 

Following are the properties of the site class:

FilesTypesToSearch:- Returns the files types you want to search

DynamicFilesTypesToSearch:- Returns dynamic files to search

BarredFolders:- Returns the barred folders

EnglishLanguage:-Returns a Boolean value whether the language is English or not.

Encoding:- Returns the encoding for the site

BarredFiles :-Returns barred files

ApplicationPath:- Assign and returns the path of the application

 

 

private static string _ApplicationPath;

 

    // Retreive FilesTypesToSearch of the site

    public static string FilesTypesToSearch

    {

        get { return ConfigurationSettings.AppSettings["FilesTypesToSearch"]; }

    }

 

    // Retreive DynamicFilesTypesToSearch of the site

    public static string DynamicFilesTypesToSearch

    {

        get { return ConfigurationSettings.AppSettings["DynamicFilesTypesToSearch"]; }

    }

 

    // Retreive BarredFolders of the site

    public static string BarredFolders

    {

        get { return ConfigurationSettings.AppSettings["BarredFolders"]; }

    }

 

    // Retreive BarredFiles of the site

    public static string BarredFiles

    {

        get { return ConfigurationSettings.AppSettings["BarredFiles"]; }

    }

 

    // Retreive EnglishLanguage of the site

    public static string EnglishLanguage

    {

        get { return ConfigurationSettings.AppSettings["EnglishLanguage"]; }

    }

 

    // Assign and retreive ApplicationPath of the site

    public static string ApplicationPath

    {

        get { return _ApplicationPath; }

        set { _ApplicationPath = value; }

    }

 

Web.config

 

The ASP.NET configuration system features an extensible infrastructure that enables you to define configuration settings at the time your ASP.NET applications are first deployed, so that you can add or revise configuration settings at any time, with minimal impact on operational Web applications and servers. Multiple configuration files, all named Web.config, can appear in multiple directories on an ASP.NET Web application server. Each Web.config file applies configuration settings to its own directory and all child directories below it. As mentioned earlier, the site configurations can be assigned in the web.config file.

 

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

<configuration>

  <appSettings>

    <!--  Place the names of the files types you want searching in the following line sepeararted by commas -->

    <add key="FilesTypesToSearch" value=".htm,.html,.asp,.shtml,.aspx" />

    <!--  Place the names of the dynamic files types you want searching in the following line separated by commas -->

    <add key="DynamicFilesTypesToSearch" value=".asp,.shtml,.aspx" />

    <!-- Place the names of the folders you don't want searched in the following line spearated by commas-->

    <add key="BarredFolders" value="cgi_bin,_bin,bin,_vti_cnf,_notes,images,scripts,CSS,App_Code,App_Data,searchimages" />

    <!-- Place the names of the files you don't want searched in the following line spearated by commas include the file extension-->

    <add key="BarredFiles" value="adminstation.htm,no_allowed.asp,AssemblyInfo.vb,Global.asax,Global.asax.vb,SiteSearchResults.aspx,Web.config" />

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

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

  </appSettings>

  <system.web>

 

     <compilation defaultLanguage="C#" debug="true" />

 

  </system.web>

 

</configuration>

Tags:

View Live Demo Download Source

Back to Main Top of Page

Search Resources

Subscribe Free Resource