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

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


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}


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}


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");}