How to Implement Full-Text Search in ASP.NET MVC - Another Example
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.
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.
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.
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.
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.
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.
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.
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.
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, 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.
Good luck in your MVC Search adventure!