404 Page Finder for Multi-Site Umbraco Installations Blog post

Out-of-the-box Umbraco helpfully let's you define a custom 404 Not Found page by configuring the Node ID of the page you wish to use in the /config/umbracoSettings.config file. This works great for when you have just one site in an installation. But what happens if you have a multi-site Umbraco set-up? For instance, if you created a multilingual site where each site has it's own home page and you want the 404 Page to be translated into the site's language? I'm talking about a structure like this:

Content

Home GB

Pages

Home FR

Pages

Home DE

Pages

What you want in this instance is to have a different 404 Page for under each of the three sites. But Umbraco will only let you have one! How do you get around this? 

IContentFinder to the Rescue!

Luckily Umbraco is very extensible and the clever developers added an interface called IContentFinder. This is very simple and has one method you need to implement:

public interface IContentFinder
{
  bool TryFindContent(PublishedContentRequest contentRequest);
}

Essentially what this does is allow you to intercept the current published content request and do something with it. In our case what we want to do is handle the instance when it is NULL - that is, when Umbraco can't find any Published Content to serve. This is where we want to intervene and serve up our custom 404 page - but one related to the site the viewer is in. So if they are in the French (FR) site they get the French version, not the English (GB) version.

The way we are going to do this is by creating a new Document Type called PageNotFound (though feel free to use whatever name you want) - the main thing is that you just have one instance of this document type per site placed under the home page. That way we easily find it relative to the site the user is in, using a simple query. After that it's just a case of creating the custom IContentFinder to find the correct instance and registering it in the OnApplicationStarting event.

Show Me Some Code!

OK, enough theory, here's some code to show this in action. You can easily adapt this to your own needs.


3 Comments


Pedro Cruz Avatar

Very helpful link!!!


But I had to put the ApplicationStarting method inside a WhateverNameClass : ApplicationEventHandler class to make it work


thanks a lot!


David Amri Avatar

Nice, but is it possible to redirect to the custom 404 page if the node exists but has no template assigned to the node? With this code I will get a IIS error instead of the custom 404 page in this case.


Dan Diplo Avatar

Hi David,


Not sure why you wouldn't have a template for your 404 page? Is there a reason?


You may be able to use a RenderMvcController to intercept the request and use that to return an ActionResult instead, but not sure what you would be gaining?


 

Just fill in the form and click Submit. But note all comments are moderated, so spare the viagra spam!

Tip: You can use Markdown syntax within comments.