Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 664
VoidKing Avatar asked Jul 30 '13 21:07

VoidKing


People also ask

Which is used for configuring the custom error page to custom error code in web config file?

webServer> in web. config is used to configure IIS level errors (IIS 7+). It overrides the configuration of <customErrors> section. To display a custom error page with an appropriate error code, use the <httpErrors> section only, and do not use the <customErrors> section.


1 Answers

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>
like image 65
Mike Brind Avatar answered Oct 31 '22 16:10

Mike Brind