Sitefinity Toolkit: Custom 404 Page with Search Results

By in
No comments

I realize that in my last post on Forcing Lowercase Urls I said I would be going over the Smooth Menu feature of the Sitefinity Toolkit. However, as I started writing it, I discovered it was a little more complicated than I expected, so I’m going to hold off on that one until I have a little more time to complete the full instructions. Sorry about that!

So instead, I’m going to quickly go over how to use the Custom 404 Page feature. If you’re a repeat visitor to my site, you may be familiar with how this works, as I used a lot of the same code from my Custom 404 Page with Related Links post. This wraps all of the code into a custom page class that you can use to easily add a 404 page to your site.


Make sure you’ve downloaded the Sitefinity Toolkit and installed it to your site’s bin directory.

You must also have a Search Index created for your site and have already indexed your site at least once. NOTE the current release will only index the FIRST search index (alphabetically sorted). If you have multiple indexes, only the first index will be searched to generate results.

You need to add a custom page to your site, I usally just call it 404.aspx and place it at the site root. Go to the page code-behind and change the base class from System.Web.UI.Page to instead use the custom 404 page class:

public partial class _404 : SelArom.Net.Sitefinity.Pages.Error404


Next you need to include a Repeater to show the search results on the front-end of the page. Add the following html:

<asp:Repeater ID="ResultsRepeater" runat="server">
        <h2>Possible Related Links</h2>
        <dl class="searchResults">
        <dt><strong><a href='<%#Eval("Url")%>'>
            <em><a href='<%#Eval("Url")%>'>
                <%#Eval("Url")%></a> </em>

You can change the ID, as well as the template html to use ul, ol, or any other markup you wish. Just be sure not to change the Url, Title, and Snippet properties, as that is required for the results to show.

Now you simply need to bind the results (loaded automatically into the SearchResults property of the base class) to this repeater:

protected void Page_Load(object sender, EventArgs e)
    if (SearchResults.Count == 0)
        ResultsRepeater.Visible = false;
        ResultsRepeater.DataSource = SearchResults;

That’s all there is too it. The base class also automatically modifies the response code to 404, ensuring that the page is properly returned as Not Found.

By default, a maximum of 5 results are loaded. If there are no related results, the Repeater is hidden from view. Note: There is currently no way to modify this, but I hope to add such a feature in a future release, as well as support for multiple, selectable indexes.

Let me know how it works for you. If you enjoy my work or have any suggestions for improvement, please consider donating your support to expanding my studio (see sidebar for details).

The following two tabs change content below.


Senior Developer at iD Tech
Josh loves all things Microsoft and Windows, and develops solutions for Web, Desktop and Mobile using the .NET Framework, Azure, UWP and everything else in the Microsoft Stack. His other passion is music, and in his spare time Josh spins and produces electronic music under the name DJ SelArom. His other passion is music, and in his spare time Josh spins and produces electronic music under the name DJ SelArom.