Creating Custom Error Pages with ASP.Net
We have all seen the white, yellow and red "Server Error in Application" messages in ASP.Net, but there are ways of preventing this screen from showing and displaying a custom error page with a more customer focused message.
- What is ASP.Net?
- ASP.Net for PHP Developers
- Creating a Simple ASP.Net Page
- ASP.Net Website Navigation Using a SiteMap
- Using ASP.Net Master Pages and Content Pages
- Validating Input using ASP.Net
- Tracing and Remote Debug in ASP.Net
- Creating Custom Error Pages with ASP.Net
- Managing Session State in ASP.Net
- Using Themes and Skins in ASP.Net
- Creating User Controls in ASP.Net
- Difference between ASP.Net User and Custom Server Controls
- Creating Fully Themable Websites with ASP.Net
- Ultimate Guide to the Web.Config File
- Adding StyleSheets to ASP.Net via C#
- Extending the Web Sitemap Xml Document
Firstly, exception handling can be used to prevent the code getting into a state that it cannot recover from. In the rare circumstance that an unhandled error occurs the page can be set to redirect to a custom error screen.
There is a setting in the web.config to redirect errors for remote users (if you view the page on localhost web server then you can see the error and debug the code accordingly).
<customErrors defaultRedirect="myErrorPage.aspx" mode="RemoteOnly"/>
If you do not wish errors to be shown at all, not even on localhost, then set the mode to On instead of RemoteOnly.
You can also set custom error pages for specific errors that your code cannot process, for example, a 401 unauthorised, 404 Not Found page, or a 500 internal error. Simply add extra lines and change the attributes to meet your needs.
<customErrors mode="On"> <error statusCode="404" redirect="/errorpages/404.aspx" /> <error statusCode="500" redirect="/errorpages/500.aspx" /> </customErrors>
Last updated on: Friday 23rd June 2017
There are no comments for this post. Be the first!