Link to home
Start Free TrialLog in
Avatar of tentavarious
tentavarious

asked on

Displaying error message on custom error page asp.net 1.1?

Experts, I  have a web application i built with asp.net 1.1 using visual studio 2003.  In this application I edited the webconfig file to point to a custom Error page if for some reason the webpage faults.    

 <customErrors defaultRedirect="Error.aspx" mode="On">
 </customErrors>
The Error.aspx page just displays a general error message with contact information.  I want it to display the actual error with maybe a general message.  Is there a global session variable i can call up that contains this error?  
Avatar of Amandeep Singh Bhullar
Amandeep Singh Bhullar
Flag of India image

When you specify the custom error On, you will be redirected to the error page but actual error will not be displayed there.
That you need to test on the local machine.
Avatar of tentavarious
tentavarious

ASKER

I am confused, how do i get the actual error message to show up on my custom error page?  For example i ran into a error where my http request was timing out, but when the error occured, the user was directed to my error page where it just said contact so and so.  I would like to have the actual error message show up on the error page.  For example the error page show show Error: http request time out, contact so and so.  There has to be a way to grab the actual error message.
I didn't think this would be that hard.  this is what i am attempting in my global.asax file.  First i changed my webconfig as follows:
  <customErrors  mode="Off">
 </customErrors>

Then i am trying to let my application_error event from the global file call up my error page.  The problem i am having is i cant get the actual message passed to the error page, only the location of the page causing the problem.  How can i just pass the message?

'This code doesnt pass the actual error message only the location of the webpage causing the error.
 Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        Dim ex As Exception = Server.GetLastError.GetBaseException
     
        Response.Redirect("error.aspx?p_error=" & ex.Message & "")
    End Sub
I'm not sure what your goal is.  Do you want your end users to see the errors or is this only for you to see for debugging purposes?  Generally speaking, you don't want to display the real error messages to your end users.  You want them to see a friendlier message while you get the messy details.

Are you using Try/Catch structures to handle errors in your code?  If so, then you will have access to the error object and all its details.  You can then do whatever you want with it such as display them to the screen, send you an e-mail, log the error, etc.
All my websites are internal and yes I dont care if they see the error, I have a friendly message on the top and i want the real error on the bottom.  I have way too many pages developed and i dont want to get an email or create a log entry when something doesnt work.  It is just as easy for the user to tell me the error.   Try catch doesnt always work, for example today my http request timed out  and the try catch didnt catch it.  I am trying to send the error from the Global on error event to my custom error page.
In the Error.aspx.cs, you will need do this in the page_load

        Exception exception = Server.GetLastError();
        if (exception != null) {
            Label1.Text = exception.GetBaseException().Message; // Label1 is the asp:Label control to
                                                                                                     // display error message
        }

Also, in web.config

        <customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="Error.aspx">
        </customErrors>

You will need to make sure, the Error.aspx does not get cached by browser. redirectMode=ResponseRewrite avoids a roundtrip (like Server Transfer), thereby retaining the LastError value.
I got an error saying that redirectMode was an unrecognized attribute.
Am i doing this correct, in my webconfig file?
   <customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="Error.aspx">
        </customErrors>

I keep getting the asp.net error page saying unrecognized attribure 'redirectMode'
I guess the problem is due to the .NET framework version (you will need 3.5 SP1 for this). Here is an article on that at http://blog.turlov.com/2009/01/search-engine-friendly-error-handling.html
So with asp.net 1.1 there is no way to do this?
I think i found out how i can  do it.  I am using my global file under the Application_Error method to do a server transfer, this seems to work.  The problem i am having now is that i cant get the correct exception to show up.  When i use ToString, i get a big long message i just want this part of the message:

"System.Web.HttpException: Exception of type System.Web.HttpException was thrown. ---> System.IO.FileNotFoundException: c:\inetpub\wwwroot\BobcatProduction\tege.aspx







Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when an error occurs
        Dim ex As Exception
        ex = Server.GetLastError
        Dim erroval As String = ex.ToString
        If Not ex Is Nothing Then
            Server.Transfer("Error.aspx?p_error=" & erroval & "")
        End If
       
    End Sub

Open in new window

The simplest approach now should be to add the exception object to Session and display the same in error page. Adding to Session should happen before Server.Transfer
Are you saying to use session variables in the global application error event?  Like so:
    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when an error occurs
        Dim ex As Exception
        ex = Server.GetLastError
            Session("set_error") = ex.Message
        Server.Transfer("Error.aspx")
    End Sub

When i tried using sessions i got this error:
Session state is not available in this context
ASKER CERTIFIED SOLUTION
Avatar of tentavarious
tentavarious

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial