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
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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
}

Method 2

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
}

Method 3

As mentioned above, but expanded to include the .NET 4.x version and assigning to the Response.RedirectLocation property when there is no Page available.

try 
{
    HttpContext.Current.Response.Redirect("~/Error.aspx");
}
catch (ApplicationException) 
{
    HttpContext.Current.Response.RedirectLocation =    
                         System.Web.VirtualPathUtility.ToAbsolute("~/Error.aspx");
}

Method 4

You should load up your ScriptManager or ScriptManagerProxy, and then check the IsInAsyncPostBack flag. That would look something like this:

ScriptManager sm = this.Page.Form.FindControl("myScriptManager") as ScriptManager;
if(!sm.IsInAsyncPostBack)
{
    ...
}

By doing this, you can mix async postbacks (which should fail to redirect) with normal postbacks, which I’m assuming you still want to redirect.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x