5

Believe it or not I tried to look for the answer to this question with a simple Google Search but I didn't find anything (Googled with "WebMatrix custom error page", "WebMatrix how to make custom server-side error page", etc.), but perhaps I am not searching with the correct terms...

Anyway, I was just wondering if there was a way (I believe it involves the web.config file) to show a custom-made error page instead of ANY server-side error page.

I know there is a way to do this with some pages (like 404 or 500) but is it possible to make a catch all page for any server-side error? (I guess 404 wouldn't work, since it has to find your site to show any custom page?)

Please forgive me if this is a repeat question, but my lack of knowledge in doing this has possibly left me without the correct search terms to search on, although I have tried searching SE, as well.

VoidKing
  • 6,282
  • 9
  • 49
  • 81

1 Answers1

10

Add the following to your web.config file within the <system.web> node:

<customErrors mode="On" defaultRedirect="~/Error.cshtml" />

This will redirect the user to Error.cshtml (which you need to create) in the event of any ASP.NET error. You can change the mode value to RemoteOnly during development, so that you can see the actual error message.

If you want a custom 404 page as well, you can do the following:

<customErrors mode="On">
    <error statusCode="500" redirect="~/Error.cshtml" />
    <error statusCode="404" redirect="~/404.cshtml" />
</customErrors>
Mike Brind
  • 28,238
  • 6
  • 56
  • 88
  • Awesome! Thanks, Mike! I had a feeling it would be pretty easy but I didn't know where to start :) What would it look like if I wanted one custom error page for every status code (not just 500), but wanted a custom 404, as well? Would it look like this: ``? – VoidKing Jul 31 '13 at 13:07
  • I'm not sure if order is important but it might be that the more specific ones need to go first. That is something you can test for yourself. – Mike Brind Jul 31 '13 at 13:13
  • Okay, that's no problem. Also, thanks for showing me the remote only option. That's very useful. I would like to let you know, though (just for the sake of the accuracy of your answer), that `remoteOnly` won't work and the 'R' actually has to be capitalized like `RemoteOnly`. It probably doesn't matter because the error page will tell developers this anyway, but just thought you might want to know in case you want to edit that tidbit. Thanks again! – VoidKing Jul 31 '13 at 13:16
  • I just had one more quick question: Considering that users would always visit our webpage remotely, and only authorized personnel should be on our server, is there any reason (for good practice, fault tolerance, or any other reason) to switch the mode value from `"RemoteOnly"` to `"On"` at all? – VoidKing Jul 31 '13 at 14:49
  • 1
    Edited the typo - thanks for spotting that. You would only set the value to `On` if you didn't want to allow people who have access to the server to see the actual error message. But of course, if they have access to the server, they may well have access to the error logs anyway... I doubt there's any significant impact on performance either way. – Mike Brind Jul 31 '13 at 16:03
  • Hey, Mike, I got around to trying to including a custom error page for both statusCode "500" and "404", but, while the first one (code 500) still works, the 404 just gives the default page. Markup: ` ` – VoidKing Sep 03 '13 at 19:58
  • Hi Voidking, i am trying to do the same thing, and am also still getting the standard 404 page. Did you find a resolution to this? – Gavin5511 Nov 01 '13 at 09:17
  • @MikeBrind Just to make something clear (for me). Those custom errors catch only ASP.NET errors i.e. aspx and cshtml pages and not regular static content errors (like htm). Am I right in this assumption? – Anderson Jan 15 '15 at 12:01
  • @Anderson You are correct in your assumption. If you want ASP.NET to handle 404 errors for static file types, you need to map them to ASP.NET in IIS. See here: http://msdn.microsoft.com/en-us/library/bb397417(v=vs.140).aspx – Mike Brind Jan 15 '15 at 14:15
  • Not sure why exactly, but I had to remove the ".cshtml" from the line in the web.config file to make it work. ie: – DanC225 Apr 13 '15 at 09:19