Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
ASP.NET: Custom Error Pages
From Wiki
Summary: Custom Error Pages
The default error pages that ASP.NET shows when an error occurs can be useful for a developer but look very unprofessional to a user. To combat this, we can make an entry in our web.config file to tell ASP.NET that when an error occurs to point the user at a custom page:
- <customErrors mode="On" defaultRedirect="ErrorPage.aspx">
- <error statusCode="404" redirect="PageNotFound.aspx"/>
- </customErrors>
Then, you just need to create "ErrorPage.aspx" with a custom error such as "An error has occurred and the development team will look into this as soon as possible".
You can also extent this functionality to automatically log the error that occurred so not only does the user get a friendly error page, you get to see the technical details of what happened. The easiest way to do this is to use the global.asax file in the root of your application. The Application_Error event will be fired when any error occurs on the site so you can use this event to log the actual error and store it somewhere e.g. a database, an email, a text file etc.
Here's an example of how to create this event:
- Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
- ' Code that runs when an unhandled error occurs
- Dim lastError As Exception = Server.GetLastError.GetBaseException()
- ' You can now use "lastError.Message" to log the actual error...
- End Sub
This Hack is part of the ASP.NET Hacks collection



LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.