Asp.net custom errors not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • janetb
    New Member
    • Aug 2006
    • 19

    Asp.net custom errors not working

    I'm trying to set up custom errors for asp.net in webconfig/global.asax on Server 2003, IIS 6. Got it working find for .aspx pages but not for any htm or html pages. So I found the article describing how to open IIS manager, click configuration, copy path for .aspx extension and add one for .htm Did that.

    Restarted website: Now I get the blank white page explaining there's a problem but not the 404 page not found
    Restarted web services: same thing

    Anybody got any tips?

    Janet
  • CroCrew
    Recognized Expert Contributor
    • Jan 2008
    • 564

    #2
    What types of errors are you looking to trap in the HTML pages?

    CroCrew~

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      I'm assuming that the OP wants to trap any error and send them to a custom "error" page. I've tried this and have never fully been successful (sometimes it works and sometimes it doesn't) so I am very curious to find out how this problem was solved.


      -Frinny

      Comment

      • CroCrew
        Recognized Expert Contributor
        • Jan 2008
        • 564

        #4
        What "programmin g" errors would be trapped in an HTML page? I know how to trap logical errors on an asp/aspx page. I just don’t know what error would be thrown in a markup language page? Maybe I am over thinking this one..

        CroCrew~

        Comment

        • CroCrew
          Recognized Expert Contributor
          • Jan 2008
          • 564

          #5
          This is what I use for my custom errors within my applications.

          In the “Global.asax” I add the following code:
          Code:
              Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
                  ' Code that runs when an unhandled error occurs
                  Server.Transfer("Errors.aspx")
              End Sub
          Here is a slimed down version of my “Errors.aspx” page:
          Code:
          <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Errors.aspx.vb" Inherits="Errors" %>
          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml">
              <head id="Head1" runat="server">
                  <title></title>
              </head>
              <body>
                 <form>
          			<div>
          				<asp:Label ID="LblMessagesAndErrors" runat="server" ForeColor="#e10000" Font-Bold="true" />
          			</div>
                  </form>
              </body>
          </html>
          Here is a slimed down version of my “Errors.aspx.vb ” page:
          Code:
          Partial Class Errors
              Inherits System.Web.UI.Page
              Dim MailFunctions As MailMan = New MailMan
          
              Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                  Dim errMessage As String = ""
                  Dim errorCode As String = "n/a"
                  Dim appException As System.Exception = Server.GetLastError()
          
                  If (TypeOf (appException) Is HttpException) Then
                      Dim checkException As HttpException = CType(appException, HttpException)
                      errorCode = checkException.GetHttpCode
                      Select Case checkException.GetHttpCode
                          Case 403
                              errMessage &= "Error 777." & checkException.GetHttpCode & ": You are not allowed to view that page."
                          Case 404
                              errMessage &= "Error 777." & checkException.GetHttpCode & ": The page you have requested can't be found."
                          Case 408
                              errMessage &= "Error 777." & checkException.GetHttpCode & ": The request has timed out."
                          Case 500
                              errMessage &= "Error 777." & checkException.GetHttpCode & ": The server can't fulfill your request."
                          Case Else
                              errMessage &= "Error 777." & checkException.GetHttpCode & ": The server has experienced an error."
                      End Select
                  Else
                      errMessage &= "Error 777.0.000: The server has experienced an error."
                  End If
          
                  LblMessagesAndErrors.Text = errMessage & "<br /><p>We are sorry for this inconvenience.</p><p>Please try back.</p>"
          
                  Dim errEmailMessage As String = "<br/ >&nbsp;<br/ >" & Request.UrlReferrer.ToString() & "<br/ >&nbsp;<br/ >" & Now()
          
                  Try
                      MailFunctions.SendEmail("CroCrew@None.com", "[~~~] ERROR www.MySite.com ERROR [~~~]", "[" & errorCode & "] " & appException.ToString & errEmailMessage)
                  Catch ex As Exception
                      ' just catching.
                  End Try
          
                  Server.ClearError()
              End Sub
          
          End Class
          Every time an error outside of a try/catch is thrown it goes to this Error page and an email gets sent to me with information that I can use to start debugging the problem. Also, if the “Case Else” is used within the “Select Case checkException. GetHttpCode” I then can add to that case statement a new code if I want too.

          I should say that I have a class that handles my sending of email called “MailMan”.

          This along with trapping my code within “try/catch”s seems to handle all my custom error handling.

          Hope this helps,
          CroCrew~

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            There's a setting in the web.config file that you can configure so that whenever an error occurs the user is directed to a custom error page.

            You can also use the global.asax file to trap any errors that cannot be handled by your page code ...and in there you can manually redirect the user to a friendly custom error page.

            I think the problem happens with how the server is configured because sometimes the server ignores the web.config's settings to redirect to custom error pages and it uses its own default pages.

            I'm pretty sure it's a server configuration but I am not a very good administrator so I don't know the answer....

            Especially with regards to this question because HTML pages are not really part of an ASP.NET application.... I mean: I have no idea how to configure an ASP.NET website (or the server hosting the website) to know to redirect to an error page if the user is trying to access an HTML page.



            -Frinny

            Comment

            Working...