MVC
Smartsite 7.10 - ...
Purpose
Steps are listed and source code is included allowing to implement a search application using a Microsoft Model-View-Controller (MVC) application.
Environment
- Microsoft .NET
- Visual Studio 2017
- MVC
- C#
- JavaScript
Steps
Create a new solution.
- In Visual Studio create a new solution and project of type ASP.NET Web Application (.NET Framework). Select MVC and accept all defaults.
- Build and run a first time. Expect a demo site.
- The solution includes several NuGet-restored packages. Optionally take measures to use Nuget packages shared amongst solutions and/or take measures to prevent storage of NuGet packages in source control.
Add Enterprise Search assemblies to the solution.
- Add Smartsite.EnterpriseSearch.Base. Add the project (if available) to the solution, or add a reference to the assembly in the web application. See .net assemblies; a signed assembly can be obtained from the installation media.
- Likewise add Smartsite.EnterpriseSearch.Search, as a .csproj project or as a .dll assembly reference.
- Build and run.
Configure the Web.config. Add application settings, adjusting as necessary:
XML | Copy Code |
---|---|
<appSettings> <add key="enterprisesearch.elastic.endpoint" value="http://localhost:9200"/> <add key="enterprisesearch.search.indexnames" value="demo1,demo2"/> </appSettings> |
- endpoint: this setting specifies which ES instance to search. This instance is populated by a Smartsite instance that obtains content management items, web site pages, document files and more, and sends these items as ES documents to Elastic Search.
- indexnames: this setting lists the names of the indexes to search, also as populated by the Smartsite instance.
Add an MVC controller.
- In the web application project, folder Controllers, Add Controller, select the empty MVC 5 controller, name it SearchController. SearchController.cs is included in the attached SearchDemoMvc.zip. Use that content. Purposes of the controller are
- To accept a search specification, to perform a search, and to return the search result.
- To accept a term-as-you-type, and to return an autocomplete list of terms for that.
- Add reference to System.Configuration.dll. Build and run.
Add an MVC view.
- Folder Views, subfolder Search (added when adding the controller), new view, name it Index, Select Empty (without model), accept defaults. Index.cshtml is included in the attached zip. The purpose of the view is to implement the user interface for the above functions of the controller.
The view requires JavaScript Search.js for the autocomplete.
- In Views\Shared\_Layout.cshtml add, after @Scripts.Render("~/bundles/jquery"): <script src="~/Content/Search/Search.js"></script>. This file is included in the zip.
Build, run and test.
- Test by visiting the site url completed with /Search.
Attachments
- SearchDemoMvc.zip, containing
- Index.cshtml
- Search.js
- SearchController.cs