Sean's Personal .NET Code Samples And References

ASP.NET setting up custom error pages in Visual Basic

Here is the code for the web.config file.

You could use multiple error pages here, one for each status code, 
    but I like to keep it to one, I always end up with enough pages in my sites.
    
So, I just append the status code the querystring and change the message on the page accordingly.

Setting mode=”RemoteOnly” you don’t get the redirect when working locally, 
    giving you a chance to correct the error where it is not displayed to the public.

<customErrors mode="RemoteOnly" defaultRedirect="error.aspx?statusCode=000">
  <error statusCode="400" redirect="error.aspx?statusCode=400" />
  <error statusCode="401" redirect="error.aspx?statusCode=401" />
  <error statusCode="403" redirect="error.aspx?statusCode=403" />
  <error statusCode="404" redirect="error.aspx?statusCode=404" />
  <error statusCode="408" redirect="error.aspx?statusCode=408" />
  <error statusCode="500" redirect="error.aspx?statusCode=500" />
</customErrors>

Here is the server side code for the custom error page file.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    'USING A TRY CATCH TO HELP PREVENT AN ENDLESS LOOP
    Try
        'DECLARE THE ERROR MESSAGE STRING
        Dim errMessage As String = ""
        
        'CHECK THAT WE HAVE A STATUS CODE TO PREVENT AN ENDLESS LOOP HERE 
        If Request.QueryString("statusCode") IsNot DBNull.Value Then
            
            'CHANGE THE MESSAGE DEPENDING ON THE ERROR
            Select Case Request.QueryString("statusCode")
                Case 400
                    errMessage &= "bad request. The file size is too large."
                Case 401
                    errMessage &= "you are not authorized to view this page."
                Case 403
                    errMessage &= "you are not allowed to view that page."
                Case 404
                    errMessage &= "the page you have requested can't be found."
                Case 408
                    errMessage &= "the request has timed out."
                Case 500
                    errMessage &= "the server can't fulfill your request."
                Case Else
                    errMessage &= "the server has experienced an error."
            End Select
        End If
        
        'DISPLAY THE MESSAGE ON THE PAGE
        ErrorMessage.Text = errMessage
    Catch ex As Exception
        'DISPLAY THIS ERROR IF SOMETHING ABOVE HAPPEND TO BREAK
        ErrorMessage.Text = ex.Message.ToString
        Context.ClearError()
    End Try
End Sub

Sub Page_Error(ByVal src As Object, ByVal args As EventArgs) Handles MyBase.Error
    'IF THIS PAGE HAS AN ERROR IT SHOULD END UP HERE 
    'IN A LAST EFFORT TO PREVENT AN ENDLESS LOOP
    Dim ex As System.Exception = Server.GetLastError()
    ErrorMessage.Text = ex.Message.ToString
    Context.ClearError()
End Sub

Here is what I have for my custom error page file.   check it out

<h1>Oh *%!#&@... we made a dooty.</h1>
<pre>
Sadly an error occured when we tried to process your request, 
    perhaps our trained monkeys simply can't keep up. 
</pre>
<table>
    <tr>
        <td>
        Don't worry, <asp:Literal runat="server" ID="ErrorMessage" /> 
            We're sorry for the inconvenience
                was our fault, not yours. 
        </td>
        <td><img src="THIS WOULD BE YOUR IMAGE PATH" width="342" height="321" alt="PUT SOMETHING HERE" /></td>
    </tr>
</table>
Sean Marcellus
There are 10 kinds of people e in this world, those who understand binary and those who don’t.