Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid "Response.Redirect cannot be called in a Page callback"

I'm cleaning up some legacy framework code and a huge amount of it is simply coding by exception. No values are checked to see if they are null, and as a result, copious amounts of exceptions are thrown and caught.

I've got most of them cleaned up, however, There are a few error / login / security related framework methods that are doing Response.Redirect and now that we are using ajax, we are getting ALOT of "Response.Redirect cannot be called in a Page callback." And I'd like to avoid this if at all possible.

Is there a way to programatically avoid this exception? I'm looking for something like

if (Request.CanRedirect)
    Request.Redirect("url");

Note, this is also happening with Server.Transfer, so I'd like to be able to check if I am able to do Request.Redirect OR Server.Transfer.

Currently, its simply doing this

try
{
    Server.Transfer("~/Error.aspx"); // sometimes response.redirect
}
catch (Exception abc)
{
    // handle error here, the error is typically:
    //    Response.Redirect cannot be called in a Page callback
}
like image 577
Allen Rice Avatar asked Oct 08 '09 15:10

Allen Rice


People also ask

Does response redirect stop execution?

Redirect has EndResponse value is true. Response. Redirect("Default. aspx", false) means current page execution is not terminated and code written after the Response.

How do I use response redirect?

Response. Redirect sends an HTTP request to the browser, then the browser sends that request to the web server, then the web server delivers a response to the web browser. For example, suppose you are on the web page "UserRegister. aspx" page and it has a button that redirects you to the "UserDetail.

What is response redirect in VB net?

Response. Redirect() sends a redirection header to the client , and the client itself requests the new page while the Server. Transfer method for the current request, terminates execution of the current page and starts execution of a new page using the specified URL path of the page.


2 Answers

You can try

if (!Page.IsCallback)
    Request.Redirect("url");

or if you dont have a Page handy...

try
{
    if (HttpContext.Current == null)
        return;
    if (HttpContext.Current.CurrentHandler == null)
        return;
    if (!(HttpContext.Current.CurrentHandler is System.Web.UI.Page))
        return;
    if (((System.Web.UI.Page)HttpContext.Current.CurrentHandler).IsCallback)
        return;

    Server.Transfer("~/Error.aspx");
}
catch (Exception abc)
{
    // handle it
}
like image 117
Brandon Avatar answered Oct 24 '22 09:10

Brandon


I believe you can simply replace Server.Transfer() with Response.RedirectLocation() which works during callback.

try
{
    Response.RedirectLocation("~/Error.aspx"); // sometimes response.redirect
}
catch (Exception abc)
{
    // handle error here, the error is typically:
    //    Response.Redirect cannot be called in a Page callback
}
like image 29
Fredrik Eder Avatar answered Oct 24 '22 10:10

Fredrik Eder