How to redirect to an error page from server side after an errorduring an AJAX post?

  • Thread starter Thread starter JimLad
  • Start date Start date
J

JimLad

Hi,

I am so sorry to post this but I am utterly confused by this. I know
it's been asked a million times before but I just don't get it.

Click a button in an UpdatePanel. Goes to server side. Server side
errors. Exception is caught in Application_Error in Global.asax. Error
is logged... Error is cancelled.

When I try to redirect to an error page I am getting a
pagerequestmanagerservererrorexception instead.

Now - how do I redirect the ENTIRE page to an error page?
Response.Redirect doesn't seem to work, presumably because I'm
returning the request to an XMLHTTP object not the main page...
although I am a little confused as I thought I had this working.

Current code is:

Sub Application_Error(ByVal sender As Object, ByVal e As
EventArgs)
Dim referenceNumber As String
Dim friendlyMessage As String = Nothing
Dim severityLevel As ExceptionSeverity

If Context IsNot Nothing Then
Dim errMsg As String = String.Empty
Dim currentException As Exception = Server.GetLastError
().GetBaseException()

Dim additionalData As String = "Error caught in
Application_Error event in Global.asax." & vbCrLf

ErrorHandler.GetFriendlyMessageAndSeverityLevel
(currentException, friendlyMessage, severityLevel)

'Log error and get reference number.
referenceNumber = LogHandler.WriteException
(currentException, severityLevel, additionalData)

'Replace placemarker {0} with referenceNumber in friendly
message.
friendlyMessage = String.Format(friendlyMessage,
referenceNumber)

Session(SESSION_ERRORMESSAGE) = friendlyMessage

'Must clear error to stop an Unhandled Exception message
appearing in the Application Event Log.
Server.ClearError()

'Redirect to error page (do not endResponse as that stops
the Session variable from working)
Response.Redirect(Request.ApplicationPath & "/
PenscopeErrorPage.aspx", False)
End If
End Sub

Cheers,

James
 
To answer my own question:

Something a bit different happens for AJAX pages.
Essentially the Server.ClearError() does NOT clear the error as far as
AJAX is concerned.
But AJAX also uses customErrors web.config section by default so as
long as customErrors mode="On" and defaultRedirect is the same as the
redirect in the Application_Error sub then everything works as
expected.
The session variable still works so the user gets a bespoke error
message either way.

James
 
Sorry I had to delete the question. Here it is again:

Hi,

I am so sorry to post this but I am utterly confused by this. I know
it's been asked a million times before but I just don't get it.

Click a button in an UpdatePanel. Goes to server side. Server side
errors. Exception is caught in Application_Error in Global.asax.
Error
is logged... Error is cancelled.

When I try to redirect to an error page I am getting a
pagerequestmanagerservererrorexception instead.

Now - how do I redirect the ENTIRE page to an error page?
Response.Redirect doesn't seem to work, presumably because I'm
returning the request to an XMLHTTP object not the main page...
although I am a little confused as I thought I had this working.

Current code is:

Sub Application_Error(ByVal sender As Object, ByVal e As
EventArgs)
Dim referenceNumber As String
Dim friendlyMessage As String = Nothing
Dim severityLevel As ExceptionSeverity

If Context IsNot Nothing Then
Dim errMsg As String = String.Empty
Dim currentException As Exception = Server.GetLastError
().GetBaseException()

Dim additionalData As String = "Error caught in
Application_Error event in Global.asax." & vbCrLf

ErrorHandler.GetFriendlyMessageAndSeverityLevel
(currentException, friendlyMessage, severityLevel)

'Log error and get reference number.
referenceNumber = LogHandler.WriteException
(currentException, severityLevel, additionalData)

'Replace placemarker {0} with referenceNumber in friendly
message.
friendlyMessage = String.Format(friendlyMessage,
referenceNumber)

Session(SESSION_ERRORMESSAGE) = friendlyMessage

'Must clear error to stop an Unhandled Exception message
appearing in the Application Event Log.
Server.ClearError()

'Redirect to error page (do not endResponse as that stops
the Session variable from working)
Response.Redirect(Request.ApplicationPath & "/
ErrorPage.aspx", False)
End If
End Sub

Cheers,

James
 
Sorry I had to delete the question. Here it is again:

Hi,

I am so sorry to post this but I am utterly confused by this. I know
it's been asked a million times before but I just don't get it.

Click a button in an UpdatePanel. Goes to server side. Server side
errors. Exception is caught in Application_Error in Global.asax.
Error
is logged... Error is cancelled.

When I try to redirect to an error page I am getting a
pagerequestmanagerservererrorexception instead.

Now - how do I redirect the ENTIRE page to an error page?
Response.Redirect doesn't seem to work, presumably because I'm
returning the request to an XMLHTTP object not the main page...
although I am a little confused as I thought I had this working.

Current code is:

    Sub Application_Error(ByVal sender As Object, ByVal e As
EventArgs)
        Dim referenceNumber As String
        Dim friendlyMessage As String = Nothing
        Dim severityLevel As ExceptionSeverity

        If Context IsNot Nothing Then
            Dim errMsg As String = String.Empty
            Dim currentException As Exception = Server.GetLastError
().GetBaseException()

            Dim additionalData As String = "Error caught in
Application_Error event in Global.asax." & vbCrLf

            ErrorHandler.GetFriendlyMessageAndSeverityLevel
(currentException, friendlyMessage, severityLevel)

            'Log error and get reference number.
            referenceNumber = LogHandler.WriteException
(currentException, severityLevel, additionalData)

            'Replace placemarker {0} with referenceNumber in friendly
message.
            friendlyMessage = String.Format(friendlyMessage,
referenceNumber)

            Session(SESSION_ERRORMESSAGE) = friendlyMessage

            'Must clear error to stop an Unhandled Exception message
appearing in the Application Event Log.
            Server.ClearError()

            'Redirect to error page (do not endResponse as that stops
the Session variable from working)
            Response.Redirect(Request.ApplicationPath & "/
ErrorPage.aspx", False)
        End If
    End Sub

Cheers,

James





- Show quoted text -

Try to add in the web.config file

<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule,
System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35"/>
</httpModules>

http://aspdotnetcodebook.blogspot.com/2008/03/using-responseredirect-in-aspnet-ajax.html
 
Back
Top