Implement Custom Error Pages
A set of tutorials for creating and implementating custom error pages in an Umbraco CMS project.
Last updated
Was this helpful?
A set of tutorials for creating and implementating custom error pages in an Umbraco CMS project.
Last updated
Was this helpful?
Umbraco is built on Microsoft's .NET Framework and uses ASP.NET. This provides different options when setting up custom error pages on your website.
Implementing custom error handling can make your site look more on-brand and minimize the impact that errors have on user experience. For example, a custom 404 page with helpful links or a search function can add extra value to your site.
In Umbraco, in-code error page handling refers to managing and displaying custom error pages directly through code. This method provides greater flexibility and control over how errors are handled and presented to users, especially within the context of an Umbraco site.
This article contains guides on how to create custom error pages for the most common scenarios:
This kind of error can occur when something has been removed, when a path has been changed, or when the chosen path is invalid.
This method will use a 404 page created via the backoffice.
Navigate to the Settings section.
Create a new "Document Type with Template".
Name the Document Type 404.
[Optional] Add properties on the Document Type.
In most cases, the 404 not found page would be static.
Fill out the Template with your custom markup.
Create another Document Type, but create it without the Template.
Call this Document Type Statuscodes.
Open the Structure Workspace view.
Check the Allow at root option.
Add the 404 Document Type as an Allowed child note type.
Navigate to the Content section.
Create a Statuscodes content item called Statuscodes.
Create a 404 content item under the Statuscodes content.
Once that is done, the next step focuses on setting up the appropriate configuration.
Take note of the published error page's GUID.
Open the appsettings.json
file in a code editor.
Add the Error404Collection
section to Umbraco:CMS:Content
, like shown below:
In the above code sample, replace the value for ContentKey
with the GUID from step 1.
With this approach, you can set different 404 pages for specific languages/cultures (such as en-us
, da-dk
, and so on):
Create a new .cs
file called Error404Page
at the root of the project.
Add the following code to the newly created class:
This section guides you in setting up a custom page for handling internal server errors (500 errors) in your Umbraco site. This setup works when:
A template throws an error.
A controller throws an unhandled exception.
A request hits the application, but something fails during rendering or processing.
Access the Umbraco Backoffice.
Navigate to the Settings section.
Create a new Document Type with Template called 500.
[Optional] Add relevant properties to the Document Types and define the template layout.
Fill out the Template with your custom markup.
You can skip this if you already have a structure for status code content nodes.
Add the 500 Document Type as an Allowed child node type on the Statuscode Document Type.
Go to the Content section.
Create a Statuscodes content item if one does not exist already.
Create a new content node of type 500 under the Statuscodes content node.
Now configure the application to display the 500 error page when internal server errors occur.
Create a folder called Controllers
in the root of your Umbraco project.
Create a new file called ErrorController.cs
in the Controllers
folder.
Add the following code to the file:
Add the /error/
route to the list of reserved paths in the appSettings.json
file:
Update Program.cs
to ensure the error route is triggered by unhandled exceptions:
To trigger a 500 error, change a Model.Value
property in your template. For example, if your Document Type has a property called test
you would normally render it with:
To deliberately cause an error, change it to something invalid like:
This should throw an error, triggering your custom 500 page.
When Umbraco fails to start, you may see a blank screen or receive a 500.30
or 502.5
error. These indicate the web application crashed or failed to initialize.
Instead, the web server itself (IIS, NGINX, Apache, and so on) must serve a static fallback 500 page. This page is independent of the application and helps communicate the issue to users when the site is down.
To handle these types of issues:
Configure your web server (IIS, NGINX, Apache) to serve a static HTML 500 page when the app fails to respond.
Use uptime monitoring to catch failed starts.
Check Umbraco logs in App_Data/Logs
for startup errors.
Sometimes you might experience issues with booting up your Umbraco project. This could be a brand new project, or it could be an existing project after an upgrade.
You will be presented with a generic error page when there is an error during boot.
You can replace the default BootFailed page with a custom static BootFailed.html
. Follow the steps below to set it up:
Open your project files.
Navigate to wwwroot/config/errors
If this folder does not exist already, create it.
Add a new file called BootFailed.html.
Add your custom markup to the file.
The BootFailed.html
page will only be shown if debugging is disabled in the appsettings.json
file. Debugging is handled using the Debug key under Umbraco:CMS:Hosting
:
If you set up everything correctly and the error pages are not showing correctly, make sure that you are not using
Any packages that allow you to customize redirects, or
Rewrite rules that might interfere with custom error handling.
If your code or any packages configure a custom IContentLastChanceFinder
, the settings in appSettings.json
will not be used.
It is also possible to set up a 404 error page programmatically using IContentLastChanceFinder
. To learn more about IContentLastChanceFinder
read the article.
Before following this example, follow the part. The example below will use the Page404
alias of the Document Type to find and display the error page.
Follow steps 6-9 in the section.
During startup, Umbraco relies on the ASP.NET Core pipeline. If the app crashes before this pipeline is fully initialized, it can't handle requests or serve custom error pages. That's why you can't rely on Umbraco or ASP.NET Core routing to show error content at this point as it has already failed. For more information, see the documentation.
Custom in your solution,
For common approaches to handling errors in ASP.NET Core web apps, see the article in the Microsoft Documentation.