How to Implement Full-Text Search in ASP.NET MVC - Another Example

by

This tutorial is similar to my previous tutorial (How to Implement Search in ASP.Net MVC 6) but provides an alternate method for achieving the same results: How to implement full-text search of static content in ASP.NET MVC 6, MVC 5, MVC 4 or MVC 3. The content I'm referring to is the text inside of your .cshtml files but this method can also be used to search static .html files. This article does not cover searching database data such as in SQL Server.

In this tutorial I'll be using a Visual Studio Extension called MVC Search for Visual Studio 2015 (available here). If you're using a legacy version of Visual Studio you will want to get the Visual Studio Extension for your version, e.g. MVC Search for Visual Studio 2013, etc. Let's get started!

Installing the Visual Studio Extension

If you downloaded the extension from the above link. You can install it by: Closing Visual Studio and double-clicking on the .vsix file (fig .5). If you've done this, you can skip the next paragraph.


fig .5
fig .5

If you haven't downloaded the extension, you can install it directly from Visual Studio. Simply select the Tools menu, then Extensions and Updates to open the Extensions and Update popup. In the popup make sure that Online is selected then type "_mvc search for visual studio" in the search box (fig 1). Once you've located the extension, click the Download button then the Install button.


fig 1
fig 1

Creating the Visual Studio Project

We'll start by creating a Visual Studio project. I'll be creating a Visual Studio 2015 project but the steps are nearly identical for all versions of Visual Studio. From the New Project dialog box, expand Templates and select Visual C# then Web (fig 3). You'll notice there are new project templates for _MVC Search projects, select _MVC Search Internet Application and click OK. You may get a popup asking you to configure Micorosoft Azure settings. Just click the Cancel button here.


fig 3.
fig 3.

What the MVC Search VS Extension Installs

The MVC Search VS Extension adds the following to your project: a reference to the MvcSearch10.dll, a stylesheet in the Content folder called mvcSearch.css, several views in a folder named MvcSearch, and appSettings to your web.config file. For now just keep in mind that your project contains these additional items.

Configuration

Before you can run the project, you'll need to specify your website's virtual root. Right-click on the project in Solution Explorer and select Properties. In the project properties window, click Web and locate the Project Url and make a note of it (fig 4). You'll need to enter this in a web.config setting.


fig 4.
fig 4.

Open the web.config file in the root directory and locate the <appSettings> section. You'll notice there are new entries that begin with mvcSearch_. If the entries are all in one line you may want to insert Returns to make them easier to read. For the mvcSearch_VirtualRoot entry specify your project's Url.

We can now run our project. We won't be debugging anything so we can press Ctrl+F5.

Note: If you debug the project you may receive random errors, which you can ignore. Simply click F5 to continue.

When the home page renders you'll notice a search box to the right of the menu items (fig 8). This is the mini-search page. Type "Home" into the box and press enter. This will execute a search. Since we have not yet built our search catalog, a new catalog will be built by crawling the pages in the site (fig 9) and then cached for future searches. As soon as the catalog is built the search results for "Home" are returned.


fig 8.
fig 8.

fig 9.
fig 9.
Note: Once the site catalog is created it will not be re-created unless you delete it.

Additionally, once the catalog is cached Autocomplete searches are available from the mini-search form. To run an Autocomplete search simply type a few characters (at least 3) into the mini-search form. Search results will immediately appear below the mini search form.

Note: You can enable/disable Autocomplete searches from your project's web.config appSettings.

Additional Information

Other Views

There's much more, like the Advanced Search view, which allows you more control over your search criteria. You can access the Advanced Search view by clicking Go on the mini-search without any search criteria. Alternatively, you can simply request: /MvcSearch/Search . You can also specify synonyms using the /MvcSearch/AddEditSynonym page. For more information see /Docs/MvcSearch/Index

Finally

Finally, there are lots of web.config configuration settings that you can tweak to get everything just right, the most important of which are the following:

  • mvcSearch_xmlCatalog - if true creates an XML catalog, false creates a binary catalog.
  • mvcSearch_FileFilter - | separated list of files to index.
  • mvcSearch_ignoreFile - | separated list of files to ignore (not index). Supports partial names.
  • mvcSearch_ignoreDirs - | separated list of directories to ignore.
  • mvcSearch_enableAutocomplete - if true executes search as the user types characters.

You can find more information in the online docs at /Docs/Index. You'll need to login to access the docs, so make sure to register for free at Register.

Good luck in your MVC Search adventure!

Comment Form is loading comments...