In this tutorial, you will learn everything you need to know in order to create a basic quick search component within an Episerver website. Quick searches are common in the header of most websites. The site visitors do a search and then get redirected to the main search page. If you are trying to build a quick search this article is for you 🔥🔥🔥
An example of a quick search is shown above. When building a quick search, the first question you will need to ask yourself is how you will build it? When thinking about building a quick search in Episerver, you have a few options available to you:
Episerver Find: Episerver Find is a SaaS search provider shipped by Episerver. Find offers more advanced search features, however, it is expensive.
Google Site Search: For about $100 bucks a year you can integrate the power of Google into your website. This was a popular approach but is now deprecated 😔
Third-party search provider: This might be a quick option, however, it will not integrate with Episerver
Build something custom
The best option for your project will obviously depend on your requirements:
Do you need to restrict the search by certain page types?
Filter results based on some data stored within a page
Do you need to restrict the search by pages within a particular section in the page tree
As soon as you need to do any type of filtering by content, building something custom is the best option (assuming you do not have your budget for Episerver Find), so this is the option we will take in this tutorial. First, we need to create a component that we can use to add the search onto a page. This is done by creating a block:
Top Search Block: The class below defines all the properties the search component will need:
To render the block you will also need to create a corresponding controller. I will skip the block controller code as it's normal Episerver stuff (read this to learn how to create a block controller). With a controller built we need a view model. I will use this one (with the logic in the view model):
Note how this view model, has a link property that will redirect a request to a page type of SearchPage
. This is how the component will redirect a user to the search page. Finally, we need to create a .cshtml
view to render the component:
You can now add this block onto a page and it will redirect a user to a search page created under the homepage automatically. Passing in a search term. Next, we need to define the search page type.
Search PageType: The code to define the search page type is shown below:
Search Controller: The search page will need a controller:
Note, that in the Index()
action a parameter called searchterm
term is passed in. This is how the search term is passed in
Search View Model: I added all the search pages logic in the view model, like this:
Search Page View: The search page will also need some HTML:
That is all the code you need to create a search. ALl the above code can be downloaded from a fully working demo on my GitHub here. Happy Coding 🤘