Error page setup in Umbraco

Before deploying your Umbraco website to production, you want to make sure you are not displaying the standard YSOD to users in cases of exceptions.

I wanted to make a setup that achieves the following:

  1. Show a nice error page to the user
  2. Show the page on the url where the exception happens
  3. Return the appropriate 500 status code

The first step

We add a static 500.html which will be the nicely displayed error message. Using the customErrors element in the web.config, we turn on custom error pages:

<customErrors mode="On" defaultRedirect="/500.html" />

By doing this, we reach the first goal. A nice page is displayed, but it will do a redirect to the 500.html page, which will in turn return a 200 status code. Not quite what we are looking for.

Stay on the url

Lets configure the redirectMode to show the error page on the same request instead of redirecting.

<customErrors mode="On" defaultRedirect="/500.html" redirectMode="ResponseRewrite" />

Alright, now it’s looking like something. But it’s still returning that 200 status code.

Enter aspx error pages

Thanks to Tom van Enckevort for pointing me in the direction of this blogpost, which is the basis for the rest of this setup: Custom error pages in ASP.NET MVC by Ben Foster

First, create some error pages in your site:

  • 404.aspx
  • 404.html
  • 500.aspx
  • 500.html

In the .aspx pages, set the appropriate http status code:

<% Response.StatusCode = 404;%>
<% Response.StatusCode = 500;%>

Then update your web.config file

<configuration>
	<system.web>
		<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/500.aspx">
			<error statusCode="404" redirect="~/404.aspx"/>
			<error statusCode="500" redirect="~/500.aspx"/>
		</customErrors>
	</system.web>
	<system.webServer>
		<httpErrors errorMode="Custom">
			<remove statusCode="404"/>
			<error statusCode="404" path="404.html" responseMode="File"/>
			<remove statusCode="500"/>
			<error statusCode="500" path="500.html" responseMode="File"/>
		</httpErrors>
	</system.webServer>
</configuration>

Now lets take this for a spin! Our exceptions now display the 500.aspx page, and return a 500 status. Great! Our not found pages return the 404.html page with a 404 status. Also great. But wait a minute. We want to use Umbraco built in 404 feature that lets us display a content page.

Umbraco’s 404

I will assume you are already fimilar with Umbraco, so lets set up umbracoSettings.config and point it to my not found node

<settings>
  <content>
    <errors>
      <error404>1063</error404>
    </errors>
  </content>
</settings>

There! Hold on, I’m still getting the 404.html page. To fix this we need two things.

Set the trySkipIisCustomErrors to true in umbracoSettings.config

<settings>
  <web.routing
    trySkipIisCustomErrors="true"
    internalRedirectPreservesTemplate="false" disableAlternativeTemplates="false" disableFindContentByIdPath="false"
    umbracoApplicationUrl="">
  </web.routing>
</settings>

And set the existingResponse=”Auto” in the web.config

<httpErrors errorMode="Custom" existingResponse="Auto" >
	<remove statusCode="404"/>
	<error statusCode="404" path="404.html" responseMode="File"/>
	<remove statusCode="500"/>
	<error statusCode="500" path="500.html" responseMode="File"/>
</httpErrors>

Now we will get a nice Umbraco content page for the 404. How about the media? /media/notthere.jpg just gives med a blank page now. We’ll fix that with a location tag in the web.config. In this location we want to set existingResponse=”Replace”, since we have no other response for 404 in here.

<configuration>
	<location path="media">
		<system.webServer>
			<httpErrors errorMode="Custom" existingResponse="Replace">
				<remove statusCode="404"/>
				<error statusCode="404" path="404.html" responseMode="File"/>
			</httpErrors>
		</system.webServer>
	</location>
</configuration>

Request validation errors

What happens whe we go to a dangerous url like this?

/somepage<script/>

Well, we get a runtime error page. Why is that? As far as I can tell, it’s because the error page we are trying to display is the 500.aspx. And since it is an aspx page, it will throw an exception becaus the request is dangerous.

So we add a special file for this. The request validation is actually a 400 bad request, so we add a 400.aspx file to our site, and turn off request validation in that.

<%@ Page validateRequest="false" %>
<% Response.StatusCode = 400;%>

And add that to our customErrors config

<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/500.aspx">
	<error statusCode="400" redirect="~/400.aspx"/>
	<error statusCode="404" redirect="~/404.aspx"/>
	<error statusCode="500" redirect="~/500.aspx"/>
</customErrors>

There. Nice error pages for bad requests, with the appropriate 400 http status.

Summary

With this we have almost all the scenarios covered. The only scenario I can find that does not get a nice error page is if I remove a dll from the /bin/ folder, which will then cause a Runtime Error message to be displayed.

We could use the existingResponse=”Replace”, but that would mean our custom Umbraco 404 content would not be displayed. So it’s a bit of a tradeoff.

If you want to play around with this, I’ve put the sample site on GitHub.

Let me know what you think 🙂

This sample code was written for Umbraco 7.5.8

Advertisements

2 thoughts on “Error page setup in Umbraco

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s