This is an example of one method to trap errors in a .Net application. This method uses the Application_Error event in the Global.ASAX and the customErrors node in the web.config file to send the user to 'friendly' error page. The actual error details and other information are sent to the email provided in the Application_Error event.
First, the Global.asax code:
Then, in the web.config:Code:<%@ Application Language="VB" %> <%@ Import Namespace="System.Diagnostics" %> <script runat="server"> Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs) ' Code that runs on application startup End Sub Sub Application_End(ByVal sender As Object, ByVal e As EventArgs) ' Code that runs on application shutdown End Sub Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs) '//Get reference to the source of the exception chain Dim ex As Exception = Server.GetLastError().GetBaseException() Dim messageBody As New StringBuilder Dim errMessage As String = ex.Message.ToString() Dim i As Integer = 0 '//Build message body With messageBody .Append("<p><b>" & Server.HtmlEncode(Request.Url.ToString) & "</b><ul>") For i = 1 To Request.Params.Count If Not Request.Params.Keys(i - 1) = "AUTH_PASSWORD" Then .Append("<li>" & Server.HtmlEncode(Request.Params.Keys(i - 1).ToString) & _ " = <u>" + Server.HtmlEncode(Request.Params.Item(i - 1).ToString) + "</u></li>") End If Next .Append("</ul></p>") i = 0 Do While (Not ex Is Nothing) And i < 10 .Append("<hr>" & htmlException(ex)) ex = ex.InnerException i += 1 Loop End With '//Email details of the error to support staff Dim message As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage With message .From = New System.Net.Mail.MailAddress(someemail@somedomain.com) .To.Add(New System.Net.Mail.MailAddress(someotheremail@somedomain.com)) .Subject = "Application Error - " & errMessage.ToString .IsBodyHtml = True .Body = messageBody.ToString() End With Dim client As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient() client.Send(message) End Sub Function htmlException(ByVal ex As Exception) As String Dim str As New StringBuilder With str .Append("<p><b>Message:</b><br />" & Server.HtmlEncode(ex.Message.ToString) & "</p>") .Append("<p><b>Source:</b><br />" & Server.HtmlEncode(ex.Source.ToString) & "</p>") .Append("<p><b>Target Site:</b><br />" & Server.HtmlEncode(ex.TargetSite.Name.ToString) & "</p>") .Append("<p><b>Stack Trace:</b><br />" & Replace(Server.HtmlEncode(ex.StackTrace.ToString), vbCrLf, "<br />") & "</p>") End With Return str.ToString() End Function Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) ' Code that runs when a new session is started End Sub Sub Session_End(ByVal sender As Object, ByVal e As EventArgs) ' Code that runs when a session ends. ' Note: The Session_End event is raised only when the sessionstate mode ' is set to InProc in the Web.config file. If session mode is set to StateServer ' or SQLServer, the event is not raised. End Sub </script>
This allows you to determine which page the user gets sent to. Notice the <error statusCode /> node. You can leave this blank or put as many statusCodes as you need here. Also, note the mode attribute. I like to use RemoteOnly as it allows anyone who is physically at the server to see the actual error message, but sends anyone who is logged in remotely to the error page. The other options are "On" and "Off".Code:<customErrors mode="RemoteOnly" defaultRedirect="~/Error.aspx"> <error statusCode="404" redirect="~/Error.aspx?code=404"/> <error statusCode="403" redirect="~/Error.aspx?code=403"/> </customErrors>
On Error.aspx, you can put a generic error message, or use a Select Case based on the status code passed in the 'code' querystring value, to determine what error message to display to the user.
This is just one method to handle errors in your application.



LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks