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

Discussion in 'ASP .Net' started by JimLad, Jan 26, 2010.

  1. JimLad

    JimLad Guest

    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
    JimLad, Jan 26, 2010
    #1
    1. Advertising

  2. JimLad

    JimLad Guest

    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
    JimLad, Jan 28, 2010
    #2
    1. Advertising

  3. JimLad

    JimLad Guest

    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

    On 28 Jan, 15:40, JimLad <> wrote:
    > 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
    JimLad, Jan 28, 2010
    #3
  4. On Jan 28, 4:44 pm, JimLad <> wrote:
    > 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
    >
    > On 28 Jan, 15:40, JimLad <> wrote:
    >
    >
    >
    > > 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- Hide quoted text -

    >
    > - 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
    Alexey Smirnov, Jan 28, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guadala Harry
    Replies:
    4
    Views:
    1,493
    =?Utf-8?B?aGFyaSBwcmFzYWQ=?=
    Jun 15, 2004
  2. =?Utf-8?B?b25l?=
    Replies:
    1
    Views:
    5,257
    Karl Seguin [MVP]
    Mar 8, 2006
  3. Martin
    Replies:
    0
    Views:
    445
    Martin
    Nov 7, 2006
  4. Sal
    Replies:
    1
    Views:
    362
  5. Replies:
    1
    Views:
    178
Loading...

Share This Page