Administration: Custom error pages
Adding a custom error page in your web application is crucial for your site. Otherwise, when your system fails or a page is not found, your users will see the standard IIS "yellow screen of death". The solution is to add custom error page to your web application.
Follow the steps in this article in order to configure custom error pages in your Sitefinity CMS web application.
Configure a custom error page
To configure the display of custom error pages, follow the procedure:
- Open the web.config file that is located in your project’s folder.
- Find the <system.webServer> config section
- Add the following <httpErrors> section:
<
httpErrors
errorMode
=
"Custom"
>
<
clear
/>
<
remove
statusCode
=
"403"
subStatusCode
=
"-1"
/>
<
remove
statusCode
=
"404"
subStatusCode
=
"-1"
/>
<
remove
statusCode
=
"500"
subStatusCode
=
"-1"
/>
<
error
statusCode
=
"403"
path
=
"/error-pages/403"
responseMode
=
"ExecuteURL"
/>
<
error
statusCode
=
"404"
path
=
"/error-pages/404"
responseMode
=
"ExecuteURL"
/>
<
error
statusCode
=
"500"
path
=
"/error-pages/500"
responseMode
=
"ExecuteURL"
/>
</
httpErrors
>
The <httpErrors> section above handles errors when accessing Sitefinity CMS pages.
IMPORTANT: If you have change the custom response code of the Application status page and you are using custom error pages, in the above example, instead of <httpErrors errorMode="Custom">
, you must use <httpErrors errorMode="Custom" existingResponse="PassThrough">
.
For more information, see Change the application status page response code.
This is because if the system is upgrading or restarting while a custom error page is requested, Sitefinity CMS will not be able to display the Application Status page properly, which is the expected behavior.
- Find the <system.web> config section
- Add the following <customErrors> section:
<
customErrors
mode
=
"RemoteOnly"
>
<
error
statusCode
=
"403"
redirect
=
"~/error-pages/403"
/>
<
error
statusCode
=
"404"
redirect
=
"~/error-pages/404"
/>
<
error
statusCode
=
"500"
redirect
=
"~/error-pages/500"
/>
</
customErrors
>
This <customErrors> section above handles errors when accessing ASP.NET files (for example .aspx files). The paths to the error pages are virtual relative paths.
- Save and close the web.config file.
Change the response status of the custom error page
If you plan to use any Sitefinity CMS page as a custom error page, you must change the response status explicitly. The default is 200, instead of 400 and this is misleading for the search engines and SEO practice.
To do this, perform the following:
-
Add new user control to your Sitefinity CMS web application and name it NotFoundStatusCodeSetter.
- Go to the code-behind and add the following code:
public
partial
class
NotFoundStatusCodeSetter : System.Web.UI.UserControl
{
protected
override
void
Render(HtmlTextWriter writer)
{
if
(!
this
.IsDesignMode())
{
base
.Render(writer);
Response.Status =
"404 Not Found"
;
Response.StatusCode = 404;
}
}
}
The code above sets the response statues code to 404 only on the frontend. This is handled by the IsDesignMode method. You must add the status code at a latest possible moment - in this sample, in the Render event.
- Register the user control in the backend.
For more information, see Register a new widget in the backend.
- In the Control CLR type or Virtual Path property, enter SitefinityWebApp.NotFoundStatusCodeSetter.
- In Name and in Title fields, enter NotFoundStatusCodeSetter.
- Create a 404 page and add the NotFoundStatusCodeSetter to it.
- Save your changes.
The proper response status code is displayed when requesting a page that cannot be found.