MVC : How To Set-up Your Error Pages Within 5 Minutes

Hey, this is a quick get started post. This will not cover the in's and out's of why things need to be the way they are:

Creating the following files in your webroot

  • 404.html
  • 404.aspx
  • 500.html
  • 500.aspx
Why 4 pages ?  The asp pages will be used for any .NET exceptions.  The html files will be used by IIS

Set A 404 Status In The two ASPX Files

In the 500.aspx and 404.aspx ad the following line:
  <% Response.StatusCode = 404; %>
If you do not do this the pages will return a 200 and mess up your SEO

Add/change the Custom Errors tag in your web.config

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

This will tell the .NET runtime to use these errors when an exception is thrown

Add/Change Your Http Error section

<httpErrors errorMode="DetailedLocalOnly">
 <remove statusCode="404"/>
 <error statusCode="404" path="404.html" responseMode="File"/>
 <remove statusCode="500"/>
 <error statusCode="500" path="500.html" responseMode="File"/>
This tells IIS what error pages to use. If this is confusing think someone typing in a wrong URL rather than exception.

That's IT!

Boom we're done, enjoy :)

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top
var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-35662136-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + ''; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();