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:
- Show a nice error page to the user
- Show the page on the url where the exception happens
- 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:
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.
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?
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.
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
11 thoughts on “Error page setup in Umbraco”
I usually hook up .html pages just to be on the safer side, but you want everything to be CMSable and the links in the footer and header should keep matching the rest of the site so I wrote this https://gist.github.com/StefanoChiodino/ad6f6860c235f4fd1d536fe53f887c2c to produce a static error page out of a dynamic one!
LikeLiked by 1 person
Super article, thanks 🙂
Great article indeed, and it should lead me to a solution.
However, when you say “Now lets take this for a spin! “, this is where you lose me…
I am not able to use .aspx pages… I get the below weird errors:
They seem to be VB errors!
I downloaded your project from Github, with similar results.
Any clue what might be wrong?
Severity Code Description Project
Error BC30035 Syntax error. 1_400.aspx
File Line Suppression State
C:\Umbraco\AchmeErrorSite\AchmeErrorSite\400.aspx 1 Active
I have no clue where the error BC30035 comes from…
I have a total of such 139 errors, and
Update: my website is running IE11.
Your solution works perfectly on Google Chrome and MS Edge 🙂
However, as stated above, for IE11 it returns
The website cannot display the page
Most likely causes:
•The website is under maintenance.
•The website has a programming error.
What you can try:
Hi Daniel. I think IE might be hiding the real error page. Try this: https://www.webwiz.net/kb/asp-tutorials/friendly-HTTP-error-messages.htm
Thanks for the detailed post Morten, it really shouldn’t require this much work should it!? I’ve setup a site as per your config, however, I still do not get my Umbraco page content I get the static HTML document. The site is running Umbraco 7.5.13 and running on IIS 8.5x. The web.config is as follows:
And the umbracoSettings.config is as follows:
Hi Simon. Looks like your config was stripped by wordpress. Could you put in in a gist maybe?
You could try and deploy the sample site on your server, and see if that works. And mayby upgrade it to your Umb version:
This is awesome! I had configured all of my custom error pages except for the 400 Bad Request when a request validation error is thrown due to including malicious characters. I was missing the validateRequest = false line in my 400.aspx. Thank you!
Isn’t this easier ? http://jondjones.com/learn-umbraco-cms/umbraco-developers-guide/how-to-configure-umbraco/how-to-create-a-404-page-in-umbraco-7
That is also part this solution, but it does not handle all the scenarios.
LikeLiked by 1 person
Helped me a LOT – thx 🙂