How can I make a custom error page in ASP.NET web-pages with WebMatrix?
Asked Answered
C

1

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.

Chrysalis answered 30/7, 2013 at 21:42 Comment(0)
B
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>
Baileybailie answered 31/7, 2013 at 8:20 Comment(10)
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: <customErrors mode="On"><error redirect="~/Error.cshtml" /><error statusCode="404" redirect="~/404.cshtml" /></customErrors>?Chrysalis
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.Baileybailie
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!Chrysalis
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?Chrysalis
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.Baileybailie
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: <system.web> <compilation debug="true" targetFramework="4.0"><assemblies><add assembly="System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /></assemblies></compilation> <customErrors mode="On"> <error statusCode="500" redirect="~/Error.cshtml" /> <error statusCode="404" redirect="~/Error404.cshtml" /> </customErrors> </system.web>Chrysalis
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?Heinrich
@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?Kirstenkirsteni
@Kirstenkirsteni 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: msdn.microsoft.com/en-us/library/bb397417(v=vs.140).aspxBaileybailie
Not sure why exactly, but I had to remove the ".cshtml" from the line in the web.config file to make it work. ie: <error statusCode="404" redirect="~/404" />Margrettmarguerie

© 2022 - 2024 — McMap. All rights reserved.